# Watcher, Transform UTC timestamp on the payload to local time

**URL:** <https://discuss.elastic.co/t/watcher-transform-utc-timestamp-on-the-payload-to-local-time/252279>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-monitoring, elastic-stack-alerting\
**Created:** [October 15, 2020, 10:12pm UTC](https://discuss.elastic.co/t/watcher-transform-utc-timestamp-on-the-payload-to-local-time/252279 "2020-10-15T22:12:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juan\_David\_Jaramillo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/juan_david_jaramillo/32/76831_2.png) [@Juan\_David\_Jaramillo](https://discuss.elastic.co/u/Juan_David_Jaramillo)\
**Post date:** [October 15, 2020, 10:12pm UTC](https://discuss.elastic.co/t/watcher-transform-utc-timestamp-on-the-payload-to-local-time/252279/1 "2020-10-15T22:12:38Z")

</div>

**I'm trying to convert the time to my local time, but I get an error, I need to send to the mail the real time which is GMT-5 America/Bogota**

```auto
{
  "trigger": {
    "schedule": {
      "interval": "1h"
    }
  },
  "input": {
    "search": {
      "request": {
        "search_type": "query_then_fetch",
        "indices": [
          "agenteallot*"
        ],
        "rest_total_hits_as_int": true,
        "body": {
          "size": 0,
          "query": {
            "bool": {
              "filter": [
                {
                  "range": {
                    "@timestamp": {
                      "gte": "{{ctx.trigger.scheduled_time}}||-5m",
                      "lte": "{{ctx.trigger.scheduled_time}}",
                      "format": "strict_date_optional_time||basic_time_no_millis"
                    }
                  }
                },
                {
                  "term": {
                    "monitor.name.keyword": "01-1-CAV30328203-DATOS-PPAL-2.7G-99.98"
                  }
                }
              ]
            }
          },
          "aggs": {
            "metricAgg": {
              "max": {
                "field": "summary.down"
              }
            }
          }
        }
      }
    }
  },
  "condition": {
    "script": {
      "source": "if (ctx.payload.aggregations.metricAgg.value >= params.threshold) { return true; } return false;",
      "lang": "painless",
      "params": {
        "threshold": 0.8
      }
    }
  },
  "actions": {
    "email_1": {
        "transform": {
        "script": "ZonedDateTime zdt = ZonedDateTime.parse(ctx.execution_time['@timestamp']);"},
      "email": {
        "profile": "standard",
        "attach_data": {
          "format": "json"
        },
        "to": [
          "juan.jaramillo@megadvantage.com",
          "juancho.jaramillo16@gmail.com",
          "victor.vera@megadvantage.com"
        ],
        "subject": "{{ctx.metadata.name}} 01-1-CAV30328203-DATOS-PPAL-2.7G-99.98",
        "body": {
          "text": """Buen dia, Adjunto registro del reporte.

 ⚠️ALERTA NODO!!! El siguiente enlace perdió la conexión. 

 Estado: Down🔴 

 Nombre del Enlace: 01-1-CAV30328203-DATOS-PPAL-2.7G-99.98

 🕧Hora: {{ctx.execution_time}}  

 Área: Networking 

 🔔Mensaje Alerta: El enlace ha dejado de responder. Responder correo a telecomunicaciones@udistrital.edu.co 

 Ingrese aquí para más información: https://72dfe17217744236af40cc31b704a664.us-central1.gcp.cloud.es.io:9243/s/distrital/app/uptime#"""
        }
      }
    }
  },
  "transform": {
    "script": {
      "source": "HashMap result = new HashMap(); result.result = ctx.payload.aggregations.metricAgg.value; return result;",
      "lang": "painless",
      "params": {
        "threshold": 0.8
      }
    }
  }
}

```

**this is the error I have**

```auto
{
  "watch_id": "_inlined_",
  "node": "ELuSGxpSRICZb2ZHJp4paA",
  "state": "executed",
  "user": "elastic",
  "status": {
    "state": {
      "active": true,
      "timestamp": "2020-10-15T22:11:28.938Z"
    },
    "last_checked": "2020-10-15T22:11:28.942Z",
    "last_met_condition": "2020-10-15T22:11:28.942Z",
    "actions": {
      "email_1": {
        "ack": {
          "timestamp": "2020-10-15T22:11:28.938Z",
          "state": "awaits_successful_execution"
        },
        "last_execution": {
          "timestamp": "2020-10-15T22:11:28.942Z",
          "successful": false,
          "reason": "Failed to transform payload"
        }
      }
    },
    "execution_state": "executed",
    "version": -1
  },
  "trigger_event": {
    "type": "manual",
    "triggered_time": "2020-10-15T22:11:28.942Z",
    "manual": {
      "schedule": {
        "scheduled_time": "2020-10-15T22:11:28.942Z"
      }
    }
  },
  "input": {
    "search": {
      "request": {
        "search_type": "query_then_fetch",
        "indices": [
          "agenteallot*"
        ],
        "rest_total_hits_as_int": true,
        "body": {
          "size": 0,
          "query": {
            "bool": {
              "filter": [
                {
                  "range": {
                    "@timestamp": {
                      "gte": "{{ctx.trigger.scheduled_time}}||-5m",
                      "lte": "{{ctx.trigger.scheduled_time}}",
                      "format": "strict_date_optional_time||basic_time_no_millis"
                    }
                  }
                },
                {
                  "term": {
                    "monitor.name.keyword": "01-1-CAV30328203-DATOS-PPAL-2.7G-99.98"
                  }
                }
              ]
            }
          },
          "aggs": {
            "metricAgg": {
              "max": {
                "field": "summary.up"
              }
            }
          }
        }
      }
    }
  },
  "condition": {
    "script": {
      "source": "if (ctx.payload.aggregations.metricAgg.value >= params.threshold) { return true; } return false;",
      "lang": "painless",
      "params": {
        "threshold": 0.8
      }
    }
  },
  "metadata": {
    "name": "Alerta caída enlace 01-1-CAV30328203-DATOS-PPAL-2.7G-99.98",
    "xpack": {
      "type": "json"
    }
  },
  "result": {
    "execution_time": "2020-10-15T22:11:28.942Z",
    "execution_duration": 708,
    "input": {
      "type": "search",
      "status": "success",
      "payload": {
        "_shards": {
          "total": 1,
          "failed": 0,
          "successful": 1,
          "skipped": 0
        },
        "hits": {
          "hits": [],
          "total": 5,
          "max_score": null
        },
        "took": 705,
        "timed_out": false,
        "aggregations": {
          "metricAgg": {
            "value": 1
          }
        }
      },
      "search": {
        "request": {
          "search_type": "query_then_fetch",
          "indices": [
            "agenteallot*"
          ],
          "rest_total_hits_as_int": true,
          "body": {
            "size": 0,
            "query": {
              "bool": {
                "filter": [
                  {
                    "range": {
                      "@timestamp": {
                        "gte": "2020-10-15T22:11:28.942168Z||-5m",
                        "lte": "2020-10-15T22:11:28.942168Z",
                        "format": "strict_date_optional_time||basic_time_no_millis"
                      }
                    }
                  },
                  {
                    "term": {
                      "monitor.name.keyword": "01-1-CAV30328203-DATOS-PPAL-2.7G-99.98"
                    }
                  }
                ]
              }
            },
            "aggs": {
              "metricAgg": {
                "max": {
                  "field": "summary.up"
                }
              }
            }
          }
        }
      }
    },
    "condition": {
      "type": "script",
      "status": "success",
      "met": true
    },
    "transform": {
      "type": "script",
      "status": "success",
      "payload": {
        "result": 1
      }
    },
    "actions": [
      {
        "id": "email_1",
        "type": "email",
        "status": "failure",
        "transform": {
          "type": "script",
          "status": "failure",
          "reason": "runtime error",
          "error": {
            "root_cause": [
              {
                "type": "script_exception",
                "reason": "runtime error",
                "script_stack": [
                  "zdt = ZonedDateTime.parse(ctx.execution_time['@timestamp']);",
                  " ^---- HERE"
                ],
                "script": "ZonedDateTime zdt = ZonedDateTime.parse(ctx.execution_time['@timestamp']);",
                "lang": "painless",
                "position": {
                  "offset": 43,
                  "start": 14,
                  "end": 74
                }
              }
            ],
            "type": "script_exception",
            "reason": "runtime error",
            "script_stack": [
              "zdt = ZonedDateTime.parse(ctx.execution_time['@timestamp']);",
              " ^---- HERE"
            ],
            "script": "ZonedDateTime zdt = ZonedDateTime.parse(ctx.execution_time['@timestamp']);",
            "lang": "painless",
            "position": {
              "offset": 43,
              "start": 14,
              "end": 74
            },
            "caused_by": {
              "type": "illegal_argument_exception",
              "reason": "Attempting to address a non-array-like type [org.elasticsearch.script.JodaCompatibleZonedDateTime] as an array."
            }
          }
        },
        "reason": "Failed to transform payload"
      }
    ]
  },
  "messages": []
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 12, 2020, 10:12pm UTC](https://discuss.elastic.co/t/watcher-transform-utc-timestamp-on-the-payload-to-local-time/252279/2 "2020-11-12T22:12:39Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
