Convert weatherapi.com time_epoch field to InfluxDB timestamp

I have been trying for about an hour to convert weatherapi.com time_epoch fields to InfluxDB timestamps, I must be missing something.

Screenshot-34

Did you try:

Date.parse(msg.payload.time)

?

See mdn

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

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

This is my JSONata:

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

and this is the error thrown:

"Invalid JSONata expression: The argument of the toMillis 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.

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

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?

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

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

{"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}

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

Can you not just use this ?

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

Works here

[{"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?

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

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

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

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 @smcgann99 suggestion of using the unix time_epoch , seems simplier, just multiply by 1000.

2 Likes

Yes, that would work too!

1 Like

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