# Only switch if state if OFF

**URL:** <https://discourse.nodered.org/t/only-switch-if-state-if-off/54881>\
**Category:** General\
**Created:** [8 December 2021 14:28 UTC](https://discourse.nodered.org/t/only-switch-if-state-if-off/54881 "2021-12-08T14:28:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HaVi](https://avatars.discourse-cdn.com/v4/letter/h/b3f665/32.png) [@HaVi](https://discourse.nodered.org/u/HaVi)\
**Post date:** [8 December 2021 14:28 UTC](https://discourse.nodered.org/t/only-switch-if-state-if-off/54881/1 "2021-12-08T14:28:35Z")

</div>

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?

```auto
[{"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}]

```

 ![NR](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/3/93f6be942e6a68f6c299c191a293416fbce94a71.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 December 2021 14:57 UTC](https://discourse.nodered.org/t/only-switch-if-state-if-off/54881/2 "2021-12-08T14:57:56Z")

</div>

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"

---

<div class="post-metadata">

**Author:** ![HaVi](https://avatars.discourse-cdn.com/v4/letter/h/b3f665/32.png) [@HaVi](https://discourse.nodered.org/u/HaVi)\
**Post date:** [8 December 2021 15:08 UTC](https://discourse.nodered.org/t/only-switch-if-state-if-off/54881/3 "2021-12-08T15:08:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [8 December 2021 15:27 UTC](https://discourse.nodered.org/t/only-switch-if-state-if-off/54881/4 "2021-12-08T15:27:21Z")

</div>

> [@HaVi](#):
>
> 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](https://cookbook.nodered.org/basic/route-on-context)

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [8 December 2021 16:29 UTC](https://discourse.nodered.org/t/only-switch-if-state-if-off/54881/5 "2021-12-08T16:29:27Z")

</div>

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.

```auto
[{"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":[[]]}]

```

---

<div class="post-metadata">

**Author:** ![HaVi](https://avatars.discourse-cdn.com/v4/letter/h/b3f665/32.png) [@HaVi](https://discourse.nodered.org/u/HaVi)\
**Post date:** [9 December 2021 07:37 UTC](https://discourse.nodered.org/t/only-switch-if-state-if-off/54881/6 "2021-12-09T07:37:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [7 February 2022 07:38 UTC](https://discourse.nodered.org/t/only-switch-if-state-if-off/54881/7 "2022-02-07T07:38:07Z")

</div>

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