Display the last time a message payload of a string "low" was received

I have a sensor that may only send a message via mqtt every few seconds. But will once every few days. Its payload is a string and is "low" the same topic also publishes another message normal as well. What I am wanting to do is show the date and time that the "low" message was received on either on a text node or something else. I did manage to get the message to show, however once it received normal it showed the normal time.

Any tips appreciated

I used a function node with the following.

var currentDate = new Date();
var day = currentDate.getDate();
var month = currentDate.getMonth() + 1; // Adding 1 to the month because it's zero-based
var year = currentDate.getFullYear();
var hour = currentDate.getHours();
var minutes = currentDate.getMinutes();

// Preserve the incoming payload
var incomingPayload = msg.payload;

if (incomingPayload === "low") {
    // If the incoming payload is "low," update it with the timestamp
    msg.payload = hour + ':' + (minutes < 10 ? '0' : '') + minutes + ', ' + day + '/' + month + '/' + year;
}

return msg;

I would just use standard nodes, I don't think a function node is really needed. Filter the message and only allow the payloads that return low to pass, use a switch node for this. You can format the date time to local time using $moment in a change node using JSONata or your function, and then display.
e.g.

[{"id":"4db2994f6c8eba26","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"my/topic","payload":"low","payloadType":"str","x":310,"y":200,"wires":[["d8dc687c1a4cc5c5"]]},{"id":"d8dc687c1a4cc5c5","type":"switch","z":"b9860b4b9de8c8da","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"low","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":470,"y":200,"wires":[["f8b8df1bb20327c7"]]},{"id":"9b65f4efa6eec08f","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"my/topic","payload":"normal","payloadType":"str","x":320,"y":240,"wires":[["d8dc687c1a4cc5c5"]]},{"id":"f8b8df1bb20327c7","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment().tz(\"Europe/London\").format(\"HH:mm:ss, DD/MM/YYYY\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":200,"wires":[["05ab6564ddab2f0b"]]},{"id":"05ab6564ddab2f0b","type":"ui_text","z":"b9860b4b9de8c8da","group":"8b5cde76.edd58","order":2,"width":0,"height":0,"name":"","label":"Last Low","format":"{{msg.payload}}","layout":"row-center","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":800,"y":200,"wires":[]},{"id":"8b5cde76.edd58","type":"ui_group","name":"default","tab":"8f03e639.85956","order":1,"disp":false,"width":"12","collapse":false},{"id":"8f03e639.85956","type":"ui_tab","name":"Home","icon":"dashboard","order":3,"disabled":false,"hidden":false}]

If you really ant to do it in your function use an if/else conditional.

if(incomingPayload === "low"){
 set payload.......
}else{
  msg = null;
}

Thank you will give this a go now without the function as well.

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