Resetting an input from MQTT

Hi - thanks in advance if anyone has any ideas..

I am using MQTT to pull some data into node red. The data is a count. Adding and subtracting whenever necessary. Sometimes just adding or subtracting 1, other times adding or subtracting 1000.

I want that count to reset to 0 each morning, as currently it just counts endlessly. Is there a way in which this can be done?

There are many ways to do this.
One of the easiest is to use the Inject node with the option 'at a specific time' and then use that in part of your flow to reset a flow-variable which is your 'count'.

inject_node

If your supscibing to this count data via MQTT then surely it is the source publishing to MQTT that needs to be reset or am I missing something?

Yeah - I agree and thats what I was hoping. However, unfortunately, this doesn't reset itself until it meets a certain threshold. This too isn't something that can be changed.

If you have no means of resetting this value then your opinions are limited to something like storing the incoming last value and using it as a tare offset then deducting the tare off each incoming value.

Interesting.. I think that is going to be the only viable way of doing after some further research. Any suggestions on how I could do something like that?

Flow 1 - copy last value to flow.currentCount

  • MQTT in -> change node

Flow 2 - copy flow.currentCount to flow.tareCount at desired time

  • inject -> change node

Flow 1 (additional) deduct tare from incoming payload

  • MQTT in -> change node

Sorry - lost me ever so slightly..

Got you with the MQTT in - into the first change node which brings through just the count only, rather than the time ect too. Then to another change node to change it to flow.currentCount?

chrome_3WExoyHFjV

Demo flow (use CTRL+I to import)

[{"id":"f6d053113d3d718b","type":"inject","z":"229f5611c9af1f6e","name":"increment","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"demo/machne/1/counter","x":1240,"y":480,"wires":[["169ba4e86618828b"]]},{"id":"418b0286e9dddacd","type":"mqtt in","z":"229f5611c9af1f6e","name":"","topic":"demo/machne/1/counter","qos":"1","datatype":"auto-detect","broker":"ac55cebb5e5db9e6","nl":false,"rap":true,"rh":0,"inputs":0,"x":1270,"y":300,"wires":[["0d663af1cef5a447","09fd3c4eb16599ca"]]},{"id":"c76759c6848062e7","type":"mqtt out","z":"229f5611c9af1f6e","name":"","topic":"","qos":"1","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"ac55cebb5e5db9e6","x":1570,"y":480,"wires":[]},{"id":"169ba4e86618828b","type":"function","z":"229f5611c9af1f6e","name":"increment","func":"let v = context.get(\"value\") || 0\nv++\ncontext.set(\"value\", v)\nmsg.payload = v\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1400,"y":480,"wires":[["c76759c6848062e7"]]},{"id":"0d663af1cef5a447","type":"change","z":"229f5611c9af1f6e","name":"copy payload to currentCount","rules":[{"t":"set","p":"currentCount","pt":"flow","to":"payload","tot":"msg"},{"t":"set","p":"tareCount","pt":"msg","to":"tareCount","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":1540,"y":300,"wires":[["e760c9d133d185c9"]]},{"id":"553d4775cd053fa3","type":"inject","z":"229f5611c9af1f6e","name":"7:00","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 07 * * *","once":false,"onceDelay":0.1,"topic":"tare","payload":"0","payloadType":"num","x":1230,"y":360,"wires":[["4e65434010a72b43"]]},{"id":"4e65434010a72b43","type":"change","z":"229f5611c9af1f6e","name":"copy currentCount -> tareCount","rules":[{"t":"set","p":"tareCount","pt":"flow","to":"currentCount","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":1450,"y":360,"wires":[[]]},{"id":"50f3691f74bccc6f","type":"debug","z":"229f5611c9af1f6e","name":"Tared","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1930,"y":300,"wires":[]},{"id":"e760c9d133d185c9","type":"function","z":"229f5611c9af1f6e","name":"deduct tare","func":"msg.payload = msg.payload - (msg.tareCount || 0)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1770,"y":300,"wires":[["50f3691f74bccc6f"]]},{"id":"a0fd31bbe09c0cea","type":"comment","z":"229f5611c9af1f6e","name":"test code (simulate value increasing)","info":"","x":1290,"y":440,"wires":[]},{"id":"09fd3c4eb16599ca","type":"debug","z":"229f5611c9af1f6e","name":"Input Data","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1490,"y":240,"wires":[]},{"id":"ac55cebb5e5db9e6","type":"mqtt-broker","name":"","broker":"broker.hivemq.com","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"5","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]
1 Like

Ah, perfect. Thanks Steve - greatly appreciated.

You will also need to take care of the reset event at the source of the counter. It was mentioned that it resets at a specific value; do you know the reset value and is it constant?

With the flow shown above, a reset of the source counter would result in a negative number in your tared count; is that the desired opetation?

The first thing to understand is the difference between a wrap around and a reset. A reset can occur at any time when a counter is reset to 0 (or any other number). A wrap around occurs in fixed number systems when you keep adding 1 to a number and you eventually exceed the number of bits. e.g. a 16 bit integer has a range of +- 32767 signed or 65535 unsigned so if you add 1 to 65535 the result will be 0.

I have done similar things previously in PLC code and the normal approach would be to always store the previous value and then when a new value arrives, substract the last count and sum the result with a local count value. In this way it is easy to dectect a wrap around or reset of the source count (being
a negative number) and account for that in your code.

Knowing the reset value becomes important in respect of not missing counts when a wrap around/reset occurs. If your count only ever increases by 1 with every message and you never miss a message then the reset can be handled without losing a count but otherwise, you need to know the reset value. I used to do this sort of thing regularly with encoder counts which were stored as double integers (32 bits) and so doing all the arithmatic in double integers handles the wrap around correctly.

Nick

Hi Nick, thanks for your response.

The reset value is 255 and is constant.

You make a good point, no the negative number isn't the desired result.. However to this point I haven't witnessed a negative number, but I guess there is still time..

What you are describing is exactly what I was after!

I had a go at simulating it:

[{"id":"4f485e7c.adf46","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"a47113b9.f30b5","type":"inject","z":"4f485e7c.adf46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Count","payload":"10","payloadType":"num","x":110,"y":100,"wires":[["b2dde93c.44fe18"]]},{"id":"b2dde93c.44fe18","type":"function","z":"4f485e7c.adf46","name":"","func":"var myCount = flow.get(\"SourceCount\")||0;\nvar myVar = msg.payload;\nmyCount = myCount + myVar;\nif (myCount>255.0) {\n    myCount = myCount - 256;\n}\nflow.set(\"SourceCount\",myCount);\nmsg.payload = myCount;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":100,"wires":[["ec47c4b6.f43158","e15d15ba.c9dce8"]]},{"id":"ec47c4b6.f43158","type":"debug","z":"4f485e7c.adf46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":100,"wires":[]},{"id":"e15d15ba.c9dce8","type":"mqtt out","z":"4f485e7c.adf46","name":"Source Count","topic":"SourceCount","qos":"0","retain":"false","broker":"d8dc27bd.545e48","x":590,"y":160,"wires":[]},{"id":"937518c5.3cfab8","type":"mqtt in","z":"4f485e7c.adf46","name":"SourceCount","topic":"SourceCount","qos":"0","datatype":"auto","broker":"d8dc27bd.545e48","x":120,"y":300,"wires":[["d0675f8d.863fd"]]},{"id":"d0675f8d.863fd","type":"function","z":"4f485e7c.adf46","name":"","func":"var myLocalCount = flow.get(\"LocalCount\")||0;\nvar myStoredCount = flow.get(\"StoredCount\")||0;\nvar myVar = parseInt(msg.payload);\nvar myDeltaCount = myVar - myStoredCount;\nflow.set(\"StoredCount\",myVar);\n\n\nif (myDeltaCount<0) {\n    myDeltaCount = myDeltaCount + 256;\n}\nmyVar = myLocalCount + myDeltaCount;\nflow.set(\"LocalCount\",myVar);\nmsg.payload = myVar;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":300,"wires":[["adef9a1e.cf2668"]]},{"id":"adef9a1e.cf2668","type":"debug","z":"4f485e7c.adf46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":600,"y":320,"wires":[]},{"id":"b097250a.b591a8","type":"inject","z":"4f485e7c.adf46","name":"Reset All","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":500,"wires":[["d63f63e0.ff311"]]},{"id":"d63f63e0.ff311","type":"change","z":"4f485e7c.adf46","name":"","rules":[{"t":"set","p":"LocalCount","pt":"flow","to":"0","tot":"num"},{"t":"set","p":"StoredCount","pt":"flow","to":"0","tot":"num"},{"t":"set","p":"SourceCount","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":500,"wires":[[]]},{"id":"d8dc27bd.545e48","type":"mqtt-broker","name":"","broker":"172.18.0.6","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Nick

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