Help with message formatting

Today's problem for me....

I have the msg.payload loaded with the time as an array (I think that is the right term).
So (example)
msg.payload.0 is the seconds.
msg.payload.1 is the minutes.
msg.payload.2 is the hour.
and so on.

Problem
I would prefer the values are formatted with 2 digits.
So if values are less than 10 there is a leading 0.

This is what I have to create the message:

[{"id":"314fee2e910159cc","type":"template","z":"0918ee609bf69fc7","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.7}}/{{payload.6}}/{{payload.5}} {{payload.2}}:{{payload.1}}:{{payload.0}}","output":"str","x":4570,"y":590,"wires":[["d1f86109c5af8059"]]}]

Rather basic, yes.

I have never really got the C++ command worked out and so doing it in Javascript is asking a bit much. :wink:

Someone - please.

Can you post an actual msg.payload please ? Also do you want to have 2 digits in the date ?

you could use JSONata in a change node and $formatNumber()
e.g.

[{"id":"2922ea64.0bdab6","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[1,2,3,4,5,6,7,8,9]","payloadType":"json","x":210,"y":3340,"wires":[["35c2b34b.4e1d94"]]},{"id":"35c2b34b.4e1d94","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload.$formatNumber($,\"00\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":3340,"wires":[["314fee2e910159cc"]]},{"id":"314fee2e910159cc","type":"template","z":"bf9e1e33.030598","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.7}}/{{payload.6}}/{{payload.5}} {{payload.2}}:{{payload.1}}:{{payload.0}}","output":"str","x":330,"y":3420,"wires":[["ea62070d.c1a55"]]},{"id":"ea62070d.c1a55","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":3260,"wires":[]}]
1 Like

[34,26,7,0,6,18,6,2022]

Is what I get from the NTP node

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