MQTT and Counter

I'm trying to create something, but I'm stuck :frowning:

I use MQTT as input, where I get ON/OFF msgs. What I want to do is:

  1. When ON received - Start looping max 10 times with delay of 1.5ms (user msg.delay so that first msg will go trough instantly)
  2. When counter gets to 10 - Stop looping
  3. When OFF received during counter - stop looping

The latest I got now attached below. When I inject OFF I got stuck because it has different _msg.id therefore looping from the beginning. Tried moving the counter to flow level but its not looping for me for some reason.

Please advice.

[{"id":"62fcd628.1262d8","type":"comment","z":"9bdbbdcf.846e7","name":"Camera Notify Back Camera","info":"","x":160,"y":600,"wires":[]},{"id":"a453c3bc.a5059","type":"mqtt in","z":"9bdbbdcf.846e7","name":"MQTT BackCam","topic":"Blueiris/backcam/state","qos":"2","datatype":"auto","broker":"25efe206.cae69e","x":100,"y":660,"wires":[["413c2242.e7888c","4990a5f.c766a5c"]]},{"id":"413c2242.e7888c","type":"debug","z":"9bdbbdcf.846e7","name":"MQTT In","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":420,"y":580,"wires":[]},{"id":"98251528.73f168","type":"debug","z":"9bdbbdcf.846e7","name":"Validity Out - And will be used for HTTP API","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":890,"y":800,"wires":[]},{"id":"4990a5f.c766a5c","type":"counter-loop","z":"9bdbbdcf.846e7","name":"Counter 20","counter":"il4990a5fc766a5c","counterType":"msg","reset":false,"resetValue":"value-null","initial":0,"initialType":"num","operator":"lt","termination":"10","terminationType":"num","increment":1,"incrementType":"num","x":390,"y":680,"wires":[["a77f426.c351cc"],["b246c24d.f5139","416f23.c06740dc"]]},{"id":"e9fc2e60.735f4","type":"function","z":"9bdbbdcf.846e7","name":"Check Validity","func":"if(msg.payload === \"ON\" ){\n    msg.delay = 1500\n    return msg;\n}\nif(msg.payload === \"OFF\" ){\n    msg.il4990a5fc766a5c = 100\n    return msg;\n} else {\n    return null;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":480,"y":760,"wires":[["98251528.73f168","4990a5f.c766a5c"]]},{"id":"b246c24d.f5139","type":"delay","z":"9bdbbdcf.846e7","name":"Delay 1.5s","pauseType":"delayv","timeout":"0","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":590,"y":680,"wires":[["e9fc2e60.735f4","e0efdb74.c2aa78"]]},{"id":"e0efdb74.c2aa78","type":"debug","z":"9bdbbdcf.846e7","name":"Timer Out","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":690,"y":880,"wires":[]},{"id":"a77f426.c351cc","type":"debug","z":"9bdbbdcf.846e7","name":"Counter Out Up","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":700,"y":540,"wires":[]},{"id":"416f23.c06740dc","type":"debug","z":"9bdbbdcf.846e7","name":"Counter Out Down","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":580,"wires":[]},{"id":"63abf8c9.cd5a58","type":"inject","z":"9bdbbdcf.846e7","name":"ON","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"ON","payloadType":"str","x":180,"y":500,"wires":[["413c2242.e7888c","e9fc2e60.735f4"]]},{"id":"71ee1f1e.5a666","type":"inject","z":"9bdbbdcf.846e7","name":"OFF","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"OFF","payloadType":"str","x":170,"y":540,"wires":[["413c2242.e7888c","e9fc2e60.735f4"]]},{"id":"25efe206.cae69e","type":"mqtt-broker","name":"Local","broker":"172.17.0.1","port":"1883","clientid":"NodeRed","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Instead of looping (which is (IMO) not recommended) you could simply send 10 messages through a rate limit set to 0.015seconds

Solution... (note, the rate limit is set to 1.5sec so you can test it)

The flow...

[{"id":"63abf8c9.cd5a58","type":"inject","z":"30e2e9e0.eeaab6","name":"ON","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"ON","payloadType":"str","x":190,"y":620,"wires":[["54f3f13c.24a65"]]},{"id":"71ee1f1e.5a666","type":"inject","z":"30e2e9e0.eeaab6","name":"OFF","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"OFF","payloadType":"str","x":190,"y":660,"wires":[["54f3f13c.24a65"]]},{"id":"250da023.ca8c9","type":"delay","z":"30e2e9e0.eeaab6","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1.5","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":780,"y":640,"wires":[["766a59a2.13f4a8"]]},{"id":"54f3f13c.24a65","type":"switch","z":"30e2e9e0.eeaab6","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"ON","vt":"str"},{"t":"eq","v":"OFF","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":370,"y":640,"wires":[["64c26a15.5dd314"],["dab1c85f.d2ab88"]]},{"id":"64c26a15.5dd314","type":"function","z":"30e2e9e0.eeaab6","name":"Generate 10 messages","func":"for(let i = 0; i<10; i++) {\n    node.send(RED.util.cloneMessage(msg))\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":570,"y":620,"wires":[["250da023.ca8c9"]]},{"id":"dab1c85f.d2ab88","type":"change","z":"30e2e9e0.eeaab6","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":660,"wires":[["250da023.ca8c9"]]},{"id":"766a59a2.13f4a8","type":"debug","z":"30e2e9e0.eeaab6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":950,"y":640,"wires":[]}]
1 Like

Thanks, That did the trick!

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