A way to tell when mqtt message was sent

102 views
Skip to first unread message

acestu

unread,
Oct 22, 2016, 2:08:05 PM10/22/16
to Node-RED
Hi,

I have an mqtt message coming in with temperature data which is linked to a gauge on the dashboard, when I turn off my raspberry pi and turn it on again a day later the gauges still have the temperature on them from the day before and it is miss leading, what I would like to do is say if the mqtt data does not come in at the specified sample time ie every 5 mins then set the gauge to zero, is this something that could easily be done, I have been ploughing through the node-red library looking for something that could compare the time that the message comes in to present time but I can't find anything.

Thanks
Stuart

Ben Hardill

unread,
Oct 22, 2016, 2:16:10 PM10/22/16
to Node-RED
There is no timestamp in a raw MQTT message, there just isn't room for it  (or any other info) in the message header.

I assume you are marking the messages as retained so the last one gets redelivered when node-red reconnects?

If you want messages to include a timestamp you have to add it to the payload yourself. This can then be compared to the current time using a function node to decide if the value should be displayed.

Dave C-J

unread,
Oct 22, 2016, 2:32:21 PM10/22/16
to node...@googlegroups.com

Use a trigger node . set to pass the original message, but send a 0 after 5 mins. But extend the time on every message so it never send the 0 unless it doesn't get a message.

acestu

unread,
Oct 22, 2016, 2:59:12 PM10/22/16
to Node-RED
Hi guys,

Yes I tried the trigger method but I wasn't sure how to reset the trigger if another temperature arrived, it says reset the triger if: and gives 2 options which are if msg.reset is set, or if msg.payload equals, I would have to use the first option but I have nothing that will trigger the sending of msg.reset, it's a viscous circle lol

Thanks
Stuart

Colin Law

unread,
Oct 22, 2016, 3:06:25 PM10/22/16
to node...@googlegroups.com
On 22 October 2016 at 19:08, acestu <ace...@hotmail.com> wrote:
Hi,

I have an mqtt message coming in with temperature data which is linked to a gauge on the dashboard, when I turn off my raspberry pi and turn it on again a day later the gauges still have the temperature on them from the day before and it is miss leading, what I would like to do is say if the mqtt data does not come in at the specified sample time ie every 5 mins then set the gauge to zero, is this something that could easily be done, I have been ploughing through the node-red library looking for something that could compare the time that the message comes in to present time but I can't find anything.

If you make sure you do not have the Retain flag set when you publish it to MQTT then it will (I believe) not provide a value on re-connection after startup.  The slight complication is that once it is written with Retain you have to explicitly remove the flag, it is not enough publishing new values without the flag set. To remove a retained message you can do
mosquitto_pub -t <topic> -r -n

Colin


acestu

unread,
Oct 22, 2016, 3:17:11 PM10/22/16
to Node-RED
Hi Colin,

I should explain this a bit better, I am using the node red dashboard and gauges, unlike real life gauges they stay at the last reported value, so if you lose a connection you don't really know about it and the gauge does not go to zero, all i am trying to do is if the gauge doesn't receive a temperature value in the regular interval then send it a 0 so I can see straight away that there is something wrong, sorry if it wasn't clear before.

Thanks
Stuart

On Saturday, October 22, 2016 at 7:08:05 PM UTC+1, acestu wrote:

Colin Law

unread,
Oct 22, 2016, 3:28:01 PM10/22/16
to node...@googlegroups.com
Right, ok.  I think that will need to be dealt with at the browser end. Even using a trigger to send a zero if there is no new value will not work in the case where the pi is shutdown or connection is lost, as the zero will not get sent.  I suspect it would have to be a feature added to the gauge itself.  I think you will also need to make sure not to set the Retain flag, otherwise you will still get the issue that the gauge will show the old value if node red (or the pi) is restarted, until the time the first valid value is received.

Colin

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/a0d80089-6c66-4c72-9ef3-7fa3096abdb1%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Dave C-J

unread,
Oct 22, 2016, 5:20:18 PM10/22/16
to node...@googlegroups.com

You don't need to reset the trigger, in fact you don't want to. You want to extend the time

​ ​
​to stop it sending the 0... here is a simple example... - as long as a message arrives within 5 secs then the 0 never appears

[{"id":"9441ed7e.9cd5e","type":"inject","z":"7a0f8ed.e4cd57","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":119.5,"y":344,"wires":[["29f44fba.f8296","e14a1416.e05238"]]},{"id":"e14a1416.e05238","type":"debug","z":"7a0f8ed.e4cd57","name":"","active":true,"console":"false","complete":"false","x":406.5,"y":345,"wires":[]},{"id":"29f44fba.f8296","type":"trigger","z":"7a0f8ed.e4cd57","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"5","extend":true,"units":"s","reset":"","name":"","x":248.5,"y":412,"wires":[["e14a1416.e05238"]]}]

acestu

unread,
Oct 26, 2016, 1:14:05 PM10/26/16
to Node-RED
Hi Dave C-J,

Thanks for explaining this and making things a little easier, I do appreciate it, I just extended the time to more than my sampling interval and it works fine.

cheers
Stuart
Reply all
Reply to author
Forward
0 new messages