Only switch if state if OFF

In the flow below I can manually switch a Philips Hue lamp on and off with two switches.

I now also want to use a motion sensor to switch on the lamp when there is movement. But this should only work when the lamp is off.

How can I process this in a flow?

[{"id":"6f34922e.f4f04c","type":"tab","label":"LIGHT","disabled":false,"info":""},{"id":"31d96fcb.d1205","type":"function","z":"6f34922e.f4f04c","name":"SWITCH OFF","func":"msg.payload={\n\"on\":true,\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":240,"wires":[["3071902b.77d5"]]},{"id":"80e3ef7f.34896","type":"inject","z":"6f34922e.f4f04c","name":"ON MANUAL","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":250,"y":240,"wires":[["31d96fcb.d1205"]]},{"id":"a88033f9.61d93","type":"function","z":"6f34922e.f4f04c","name":"SWITCH OFF","func":"msg.payload={\n\"on\":false,\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":160,"wires":[["3071902b.77d5"]]},{"id":"2c0e4152.43299e","type":"inject","z":"6f34922e.f4f04c","name":"OFF MANUAL","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":250,"y":160,"wires":[["a88033f9.61d93"]]},{"id":"d6feb914.b10d78","type":"inject","z":"6f34922e.f4f04c","name":"ON MOTION","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":250,"y":440,"wires":[["de3981b0.74f92"]]},{"id":"de3981b0.74f92","type":"function","z":"6f34922e.f4f04c","name":"SWITCH ON","func":"msg.payload={\n\"on\":true,\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":440,"wires":[["3071902b.77d5"]]},{"id":"61fb1609.a49f68","type":"comment","z":"6f34922e.f4f04c","name":"ONLY IF LAMP IS OFF:","info":"","x":360,"y":380,"wires":[]},{"id":"daca803c.e8b67","type":"comment","z":"6f34922e.f4f04c","name":"MANUAL SWITCHES","info":"","x":260,"y":80,"wires":[]},{"id":"3071902b.77d5","type":"hue-light","z":"6f34922e.f4f04c","name":"LAMP","bridge":"6e5f1fd.c009ce","lightid":"25","colornamer":true,"skipevents":false,"universalevents":false,"x":730,"y":240,"wires":[[]]},{"id":"6e5f1fd.c009ce","type":"hue-bridge","name":"ToHa Bridge","bridge":"192.168.2.150","key":"ipXv8iEicQTW4xXBKoANKVkdRooBojYKrV3RQ2k8","interval":"3000","disableupdates":false}]

I don't really understand. What's the problem with turning the light on when it is already on?

You can store the current status in flow.status and use a switch node to only pass motion messages if flow.status == "OFF"

The flow is a bit simplified, so it may seem illogical. The motion sensor is intended to use the lamp as an orientation light during the night and then at 10% brightness. If the lamp is already "normal" on, it should not light up at 10% brightness when movement is detected. This only has to be done when the lamp is off.

Could you be a little more specific how to store the current status in flow.status?

This helped me in a similar context

https://cookbook.nodered.org/basic/route-on-context

If you use non standard nodes in your flow it's nice if you declare them in your description. We don't load all the nodes available. Here is a take on a possible solution, untested as I didn't load the non standard nodes but should work or at least get close.

[{"id":"6f34922e.f4f04c","type":"tab","label":"LIGHT","disabled":false,"info":""},{"id":"31d96fcb.d1205","type":"function","z":"6f34922e.f4f04c","name":"SWITCH ON","func":"msg.payload={\n\"on\":true,\n};\nflow.set('Light','on');\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":240,"wires":[["3071902b.77d5"]]},{"id":"80e3ef7f.34896","type":"inject","z":"6f34922e.f4f04c","name":"ON MANUAL","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":250,"y":240,"wires":[["31d96fcb.d1205"]]},{"id":"a88033f9.61d93","type":"function","z":"6f34922e.f4f04c","name":"SWITCH OFF","func":"msg.payload={\n\"on\":false,\n};\nflow.set('Light','off');\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":160,"wires":[["3071902b.77d5"]]},{"id":"2c0e4152.43299e","type":"inject","z":"6f34922e.f4f04c","name":"OFF MANUAL","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":250,"y":160,"wires":[["a88033f9.61d93"]]},{"id":"d6feb914.b10d78","type":"inject","z":"6f34922e.f4f04c","name":"ON MOTION","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":250,"y":440,"wires":[["de3981b0.74f92"]]},{"id":"de3981b0.74f92","type":"function","z":"6f34922e.f4f04c","name":"SWITCH ON","func":"msg.payload={\n\"on\":true,\n};\nlet Z = flow.get('Light');\nif (Z == 'on'){\n    return null;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":440,"wires":[["3071902b.77d5"]]},{"id":"61fb1609.a49f68","type":"comment","z":"6f34922e.f4f04c","name":"ONLY IF LAMP IS OFF:","info":"","x":360,"y":380,"wires":[]},{"id":"daca803c.e8b67","type":"comment","z":"6f34922e.f4f04c","name":"MANUAL SWITCHES","info":"","x":260,"y":80,"wires":[]},{"id":"3071902b.77d5","type":"hue-light","z":"6f34922e.f4f04c","name":"LAMP","bridge":"6e5f1fd.c009ce","lightid":"25","colornamer":true,"skipevents":false,"universalevents":false,"x":730,"y":240,"wires":[[]]}]

Thank you all for the help. I'll continue with it later today, I'm sure it will be fine now.

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