# Convert weatherapi.com time\_epoch field to InfluxDB timestamp

**URL:** <https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859>\
**Category:** General\
**Created:** [17 September 2022 18:25 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859 "2022-09-17T18:25:28Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [17 September 2022 18:25 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859/1 "2022-09-17T18:25:28Z")

</div>

I have been trying for about an hour to convert [weatherapi.com](http://weatherapi.com) time\_epoch fields to InfluxDB timestamps, I must be missing something.

![Screenshot-34](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/8/c8919bc25c10893a33e2911b6350dfcb7cd92a81.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [17 September 2022 18:41 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859/2 "2022-09-17T18:41:27Z")

</div>

Did you try:

```auto
Date.parse(msg.payload.time)

```

?

[See mdn](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse)

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [17 September 2022 21:02 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859/3 "2022-09-17T21:02:18Z")

</div>

I'm trying to do this in a change node using the JSONata $map function.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 September 2022 21:11 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859/4 "2022-09-17T21:11:36Z")

</div>

This should return a timestamp in milliseconds `$toMillis($$.payload.time)`

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [17 September 2022 21:18 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859/5 "2022-09-17T21:18:42Z")

</div>

This is my JSONata:

```auto
$map(
   payload.forecast.forecastday[].hour,
   function($v, $i, $a) {
       
           {
               "time": $toMillis($v.time)
           }
       
   }
)

```

and this is the error thrown:

```auto
"Invalid JSONata expression: The argument of the toMillis function must be an ISO 8601 formatted timestamp. Given "2022-09-17 00:00""

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 September 2022 21:36 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859/6 "2022-09-17T21:36:13Z")

</div>

> [@ghayne](#):
>
> `llis function must be an ISO 8601 formatted timestamp. Given "2022-09-17 00:00""`

Seems to say the format is other than iso.  
Can we see a copy value of `payload.forecast.forecastday`

You could try this.

```auto
$number($moment($v.time).format("x"))

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 September 2022 06:11 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859/7 "2022-09-18T06:11:01Z")

</div>

> [@ghayne](#):
>
> I must be missing something.

Is that a JavaScript date? If so then I think you should be able to use it directly with the influx nodes. What happens if you do that?

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [18 September 2022 15:21 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859/8 "2022-09-18T15:21:32Z")

</div>

> [@Colin](#):
>
> Is that a JavaScript date? If so then I think you should be able to use it directly with the influx nodes. What happens if you do that?

```auto
"Error: Expected numeric value for, timestamp, but got '2022-09-18 00:00'!"

```

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [18 September 2022 15:35 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859/9 "2022-09-18T15:35:11Z")

</div>

> [@E1cid](#):
>
> Can we see a copy value of `payload.forecast.forecastday`

It's actually `payload.forecast.forecastday[].hour`

```auto
{"time_epoch":1663455600,"time":"2022-09-18 00:00","temp_c":4.9,"temp_f":40.8,"is_day":0,"condition":{"text":"Clear","icon":"//cdn.weatherapi.com/weather/64x64/night/113.png","code":1000},"wind_mph":4,"wind_kph":6.5,"wind_degree":357,"wind_dir":"N","pressure_mb":1024,"pressure_in":30.24,"precip_mm":0,"precip_in":0,"humidity":91,"cloud":18,"feelslike_c":3.5,"feelslike_f":38.3,"windchill_c":3.5,"windchill_f":38.3,"heatindex_c":4.9,"heatindex_f":40.8,"dewpoint_c":3.6,"dewpoint_f":38.5,"will_it_rain":0,"chance_of_rain":0,"will_it_snow":0,"chance_of_snow":0,"vis_km":10,"vis_miles":6,"gust_mph":8.5,"gust_kph":13.7,"uv":1}

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 September 2022 15:42 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859/10 "2022-09-18T15:42:47Z")

</div>

> [@ghayne](#):
>
> `e":"2022-09-18 00:00",`

So it is not in the format you showed in OG post. My last expression should parse it to a numeric millisecond timestamp.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [18 September 2022 15:45 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859/11 "2022-09-18T15:45:34Z")

</div>

> [@ghayne](#):
>
> `{"time_epoch":1663455600`

Can you not just use this ?

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [18 September 2022 15:48 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859/12 "2022-09-18T15:48:04Z")

</div>

> [@E1cid](#):
>
> So it is not in the format you showed in OG post. My last expression should parse it to a numeric millisecond timestamp.

```auto
Error: A 400 Bad Request error occurred: {"error":"unable to parse 'WeatherAPI,time=1663459200000 1663455600000': invalid field format"}

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 September 2022 16:09 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859/13 "2022-09-18T16:09:21Z")

</div>

Works here

```auto
[{"id":"3b1269.3c824598","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload.forecast.forecastday[0].hour","v":"{\"time_epoch\":1663455600,\"time\":\"2022-09-18 00:00\",\"temp_c\":4.9,\"temp_f\":40.8,\"is_day\":0,\"condition\":{\"text\":\"Clear\",\"icon\":\"//cdn.weatherapi.com/weather/64x64/night/113.png\",\"code\":1000},\"wind_mph\":4,\"wind_kph\":6.5,\"wind_degree\":357,\"wind_dir\":\"N\",\"pressure_mb\":1024,\"pressure_in\":30.24,\"precip_mm\":0,\"precip_in\":0,\"humidity\":91,\"cloud\":18,\"feelslike_c\":3.5,\"feelslike_f\":38.3,\"windchill_c\":3.5,\"windchill_f\":38.3,\"heatindex_c\":4.9,\"heatindex_f\":40.8,\"dewpoint_c\":3.6,\"dewpoint_f\":38.5,\"will_it_rain\":0,\"chance_of_rain\":0,\"will_it_snow\":0,\"chance_of_snow\":0,\"vis_km\":10,\"vis_miles\":6,\"gust_mph\":8.5,\"gust_kph\":13.7,\"uv\":1}","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":240,"y":1980,"wires":[["cdb8325f.1cb61"]]},{"id":"cdb8325f.1cb61","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$map(\t payload.forecast.forecastday[].hour,\t function($v, $i, $a) {\t {\t \"time\": $number($moment($v.time).format(\"x\")) \t } \t } \t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":2000,"wires":[["7dae4206.4a76f4"]]},{"id":"7dae4206.4a76f4","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":2080,"wires":[]}]

```

[edit] you mean sending the result to influx, what is the precision setting of your influx?

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [18 September 2022 16:36 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859/14 "2022-09-18T16:36:30Z")

</div>

> [@E1cid](#):
>
> [edit] you mean sending the result to influx, what is the precision setting of your influx?

I apologise, your solution works, I had named the field I was attempting to write to  
`time`. InfluxDB expects `timestamp`.

```auto
  payload.forecast.forecastday[].hour,
   function($v, $i, $a) {
       
           {
               "timestamp": $number($moment($v.time).format("x"))
           }
       
   }
)

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 September 2022 17:07 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859/15 "2022-09-18T17:07:43Z")

</div>

Cool.  
Just for info  
There is no need to call a map function as `.` will map  
e.g.

```auto
payload.forecast.forecastday[].hour.${
           "timestamp": $number($moment($.time).format("x"))          
       }            

```

Remove the `$` after hour if you want an array of objects returned from the `forecastday` array, rather than a single object.

I do like @Sean-McG suggestion of using the unix `time_epoch` , seems simplier, just multiply by 1000.

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [18 September 2022 18:02 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859/16 "2022-09-18T18:02:00Z")

</div>

> [@E1cid](#):
>
> I do like @smcgann99 suggestion of using the unix `time_epoch` , seems simplier, just multiply by 1000.

Yes, that would work too!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 October 2022 18:02 UTC](https://discourse.nodered.org/t/convert-weatherapi-com-time-epoch-field-to-influxdb-timestamp/67859/17 "2022-10-02T18:02:06Z")

</div>

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