Send time value via MQTT

what is the simplest method to send MQTT from nodered time values ? I need it in the form: "17:37:02 02.12.2023 SUN"
I want the last 3 characters to be the day of the week.
I have several esp8266 devices that display the clock on LED displays, or use the clock values for other tasks. Until now each of them got their values by accessing NTP server. I would like to use the raspberry pi's internal clock that hosts node red and use a mqtt out node to send the values to a topic like "system_time". Thus, if I need to manually set the clocks, I can only do it on the raspberry pi

You should use ISO8601 format 2023-12-02T17:37:02.001Z This is the "natural" stringified format of a JavaScript datetime object. It is universal and the most widely use standard that is both machine and human readable.

Note that it is always in UTC (so-called Zulu) time zone. You should always use UTC to avoid timezone and daylight savings issues. Only convert to/from local at the point of user interaction.

If you REALLY want that non ISO8601 format, then $moment in a change node can do that...

Set: msg.payload
To: $moment().format("HH:mm:ss DD.MM.YYYY ddd")


Demo flow (use CTRL+I to import)

[{"id":"ca28077d6a3cf12a","type":"inject","z":"c37c71daed7c269d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1860,"y":60,"wires":[["804015860c58bbf2"]]},{"id":"804015860c58bbf2","type":"change","z":"c37c71daed7c269d","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment().format(\"HH:mm:ss DD.MM.YYYY ddd\")","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"master/time","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1880,"y":120,"wires":[["904db767233fc91d"]]},{"id":"904db767233fc91d","type":"debug","z":"c37c71daed7c269d","name":"--> send me to MQTT","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1900,"y":180,"wires":[]}]
1 Like

that ISO8601 format does not have day of week.

thank you, that seem to be what I looking for. Is there a problem with that format ? It is just another order, first hour minutes etc...

It depends what you are going to do with it. If it is for display and that is how you want to display it, then that is fine. If you are going to do anything else with it then you should generally use a standard format such as ISO so that it can be recognised by a wide variety of software packages.

I just extract hh and mm to display on clicks and on other devices to control some relays. Day of week need for some special relay programm in some days. It realy does not matter for me the order in the mesage.

maybe in March when it switches to summer time, will it switch by itself? given that it is raspberry pi's clock

You'd be better off sending JSON values that dont need string manipulation

Something like

  hour: 15,
  minute: 22,
  second: 44,
  day, 2
  month: 12,
  year: 2023,
  dayOfWeek: "Sat",
  z: "+01:00"
1 Like

how to use this? where to put that code? I'm sorry I don't really know how to use node red, I just connect nodes to each other, write code in them I don't really know.

Does not the code you are running in the end device have the ability to accept an ISO date and then tell you the local time and day of week?

yes, but they will have to be reprogrammed to extract the information from another position in the string

That doesn't matter, you can and should derive that at point of use. Day of week is locale-specific anyway and so may not be standard.

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