Timestamp conversion going wrong

I want to convert a json(?) timestamp from, eg, 1613498523 to "human readable"; I have tried using the moment node, but whatever output format I choose, the date is right but the time is always 00:00 (or 00:00:00) - which is not what an online converter interprets the initial input as.

Where am I going wrong?

TIA

Screenshot 2021-02-16 at 18.04.49

.
.

Try entering payload.ts in the top msg input field. The node needs the timestamp. As you did not feed it one it gave you the output for epoch which is midnight jan 1st 1970 so you get 00:00:00

You can also do this in the change node.

[{"id":"52919353.50ca94","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":2560,"wires":[["ee7bd564.7ecb58"]]},{"id":"ee7bd564.7ecb58","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(payload).tz(\"London/Europe\").format(\"LTS\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":2560,"wires":[["596cd523.90c89c"]]},{"id":"596cd523.90c89c","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":790,"y":2440,"wires":[]}]
1 Like

Depending on what you mean by "human readable", you can also simply use a function node:

const ts = new Date(msg.payload.ts)
return {
  payload: ts.toISOString()
}

There are a few other native methods as well:

Date.prototype.toISOString() - JavaScript | MDN (mozilla.org)

Many thanks @E1cid & @TotallyInformation ; I have made some progress.

Using the node-red-contrib-moment Date/Time editor node I am able to change the object ts: string to a payload of, eg, "18 Feb 2021 12:32".

But the problem I now have is that when this arrives at my MQTT broker it seems to think it is a value pair(?), and each message creates a new Input, eg input name = 18 Feb 2021 12 with a value of 32.

How might I change the message to be Input name emon/mqtt_time and a value of the full time, ie 18 Feb 2021 12:32?

I don't fully follow what you are saying. Are you sending it to an MQTT broker? If so then you should send it as an ISO string and convert it to a readable form at the other end.

Is that not exactly what the second output in your debug screenshot shows? If that isn't what you want please explain again, showing what input you are getting and what output you want.

Not sure I do either TBH :grinning: But to summarise:

  • My Weather Station sends out data via a url which includes a timestamp in the format ts: 1613651565. I would like to make this human readable within a Home Assistant dashboard so that I can quickly see how "old" the last update was, and very preferably more than just the ISO8601 format, as that isn't very easy to scan read.
  • My plan was to send this info to my MQTT broker on another device on my LAN, and from there in to my Home Assistant.
  • I have a NR function node that extracts this info:
msg.payload = {
"ts":msg.payload.data.ts};
return msg;
  • It then passes on to a moment Date/Time Formatter node, but it is here that I am getting a bit stuck as even though it sends on the ISO8601 data, by the time it arrives in my MQTT broker it sees a value pair(?) and gives the Input name as eg 2021-02-18T16 and a value of 49 (due to the colon) which is not a lot of use :sweat_smile:

Many thanks,

Can't HA convert from a millisecond timestamp to human readable? It is generally best to do that sort of conversion at the point of display if possible.