Inject timestamp in MQTT message

Hi, I'm working on a project that uses MQTT node. I need to send environment sensor readings to an MQTT broker but I need to send together with the topic payload the timestamp of the reading itself in order to know the exact moment of time when the reading has been taken from the MQTT client. I found that in the MQTT protocol something like "timestamp value" as a message is possible. For instance a payload like "1555522027 34" ( where "34" is the metric value) should be accepted by the MQTT Broker. The strange thing is that while the topic and the value (34) are accepted by the Broker the timestamp seems to be discarded. The problem can be in:
A. the MQTT-out not that didn't parse the timestamp that I put in the msg.payload or
2. The MQTT Broker that discard the timestamp sent by the MQTT-out node
I'm sure that along the way the timestamp for some unknown reason get lost....
Does anyone succeed in sending timestamp in a MQTT message using the default MQTT-out node in Node-Red?

As the sidebar help for the MQTT node says, it uses msg.payload and msg.topic. It does not say anything about a timestamp. It is up to you to get msg.payload to contain the value you want to publish. There is no special concept a timestamp in mqtt.

If necessary you can send JSON as the payload.

E.g.

msg.topic = "sensor/temperature";
msg.payload = { 
  "timestamp" : Date.now(),
  "temperature" : someVar
}
return msg;

Thanks Steve I'll give it a try and let you know

image
Is there a syntax to add the stamp to the broker last will?
Im thinking it's something obvious Im just not putting together.
something something {$(Date.now())} ?

No, the LWT information is sent to the broker when the MQTT conection is first made. Then if the connection is lost, the BROKER will send out the LWT message to all devices that are subscribed to the LWT topic. To add a timestamp to the msg would mean the BROKER would have to do that work.

But if you supscribe to the topic you will get the message

2 Likes

Making the broker log the Timestamp was a big "O DUH" moment hahaha

Thank you!

1 Like