Override a flow

I'm new to node-red. I'm trying to create a flow where my hue sensor activates a scene, and after x seconds it reverts back to the previous state. But now every message gets through the whole flow. So every time the motion sensor detects motion, it triggers the flow. That's ok, but I want to stop the current message and continue with the new message.

I tried several things with variables, switches etcetera. But I can't get it to work properly.

Can somebody help me out?

[{"id":"b7ac12ab.7e528","type":"tab","label":"Hue keuken","disabled":false,"info":""},{"id":"1524c811.d2fc68","type":"switch","z":"b7ac12ab.7e528","name":"","property":"data.attributes.brightness","propertyType":"msg","rules":[{"t":"lt","v":"254","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":1050,"y":140,"wires":[["52a10aa8.2dbd14"]]},{"id":"77eb2e45.90f64","type":"hue-group","z":"b7ac12ab.7e528","name":"KeukenState","bridge":"caaebba0.2dfe58","groupid":"2","colornamer":true,"skipevents":false,"x":1030,"y":260,"wires":[["83152845.b30018"]]},{"id":"52a10aa8.2dbd14","type":"hue-scene","z":"b7ac12ab.7e528","name":"Helder keuken","bridge":"caaebba0.2dfe58","sceneid":"3kY5XJ9gXJ5YN1k","skipevents":false,"x":1240,"y":140,"wires":[["794162e4.a95c1c"]]},{"id":"d8c980b8.1085","type":"function","z":"b7ac12ab.7e528","name":"previous state","func":" var brightness=flow.get('brightness')  \n    return {\n        payload: {\n            on: true,\n            brightness: brightness\n        }\n    }\n","outputs":1,"noerr":0,"x":780,"y":260,"wires":[["77eb2e45.90f64"]]},{"id":"f1f134af.a65878","type":"api-current-state","z":"b7ac12ab.7e528","name":"","server":"824131a9.869e6","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.keuken_1","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":620,"y":140,"wires":[["e3257eef.cc8f5"]]},{"id":"e3257eef.cc8f5","type":"function","z":"b7ac12ab.7e528","name":"previous state","func":"flow.set('brightness', (msg.data.attributes.brightness/2.54));\n  return msg;","outputs":1,"noerr":0,"x":880,"y":140,"wires":[["1524c811.d2fc68"]]},{"id":"d3b645bf.069128","type":"inject","z":"b7ac12ab.7e528","name":"","topic":"","payload":"{\"active\":true,\"motion\":true,\"updated\":\"2020-01-11T01:10:01+00:00\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":180,"wires":[["fc1775ef.2c6a48"]]},{"id":"3d0bf175.1d620e","type":"delay","z":"b7ac12ab.7e528","name":"Delay","pauseType":"delayv","timeout":"1","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":570,"y":260,"wires":[["d8c980b8.1085"]]},{"id":"794162e4.a95c1c","type":"function","z":"b7ac12ab.7e528","name":"set motion true","func":"flow.set('motion_keuken', true);\nmsg.delay = 5000;\nreturn msg;","outputs":1,"noerr":0,"x":380,"y":260,"wires":[["3d0bf175.1d620e"]]},{"id":"83152845.b30018","type":"function","z":"b7ac12ab.7e528","name":"set motion false","func":"flow.set('motion_keuken', false);\nreturn msg;","outputs":1,"noerr":0,"x":1220,"y":260,"wires":[[]]},{"id":"fc1775ef.2c6a48","type":"switch","z":"b7ac12ab.7e528","name":"Check motion true / false","property":"motion_keuken","propertyType":"flow","rules":[{"t":"false"},{"t":"true"}],"checkall":"true","repair":true,"outputs":2,"x":330,"y":140,"wires":[["f1f134af.a65878"],["794162e4.a95c1c"]]},{"id":"caaebba0.2dfe58","type":"hue-bridge","z":"","name":"Philips hue","bridge":"192.168.0.108","key":"YDSZNb8M1YWitGciLD3c-9LBx869YAhTadN0XfNk","interval":"750","disableupdates":false},{"id":"824131a9.869e6","type":"server","z":"","name":"Home Assistant"}]

Here is an idea. As soon as a msg enters the flow, run it thru a change node. The change node sets a flow context variable (say 'flow.msgtime') to the current time, then sets msg.msgtime to flow.msgtime.

Now at the point you want to see if the msg is current, add a switch node to check if msg.msgtime == flow.msgtime. If it does, let it continue, if not then you know another msg has entered the flow.

Here is a sample flow demonstrating what I mean

[{"id":"e8a90827.0d99c8","type":"inject","z":"a8bc6f71.c5354","name":"","topic":"","payload":"msg 1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":100,"wires":[["cfcdc560.9745d8"]]},{"id":"6f31b8e.e8ebac8","type":"debug","z":"a8bc6f71.c5354","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":290,"y":300,"wires":[]},{"id":"cfcdc560.9745d8","type":"change","z":"a8bc6f71.c5354","name":"","rules":[{"t":"set","p":"msgtime","pt":"flow","to":"","tot":"date"},{"t":"set","p":"msgtime","pt":"msg","to":"msgtime","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":120,"wires":[["6cf68e85.704208"]]},{"id":"6cf68e85.704208","type":"delay","z":"a8bc6f71.c5354","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":280,"y":180,"wires":[["17f1cba8.d0bdbc"]]},{"id":"17f1cba8.d0bdbc","type":"switch","z":"a8bc6f71.c5354","name":"","property":"msgtime","propertyType":"msg","rules":[{"t":"eq","v":"msgtime","vt":"flow"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":270,"y":240,"wires":[["6f31b8e.e8ebac8"],[]],"outputLabels":["current","new msg in flow"]},{"id":"9a7f4394.d7ad","type":"inject","z":"a8bc6f71.c5354","name":"","topic":"","payload":"msg 2","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":140,"wires":[["cfcdc560.9745d8"]]}]

works! thank you :slight_smile:

it was easier to use the 'current_state' node to check if there is still motion. Thanks anyway!
edit:
mmm maybe your solution is better anyway, I keep on testing!

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