How to enable/disable motion flow.

Hi,

I have the following flow:

[{"id":"8fa5f04a.f6ab3","type":"mqtt in","z":"d11dfeaa.8b609","name":"Motion bijkamer","topic":"zigbee2mqtt/Bewegingsmelder_bijkamer","qos":"2","broker":"7a291092.162ac","x":1240,"y":1820,"wires":[["1dc32f82.4faf7"]]},{"id":"16f771ab.f7b76e","type":"mqtt out","z":"d11dfeaa.8b609","name":"Bijkamer lamp","topic":"zigbee2mqtt/Lamp_bijkamer/set","qos":"","retain":"","broker":"7a291092.162ac","x":1980,"y":2000,"wires":[]},{"id":"7e36cfcf.49ce3","type":"change","z":"d11dfeaa.8b609","name":"Zet aan (255)","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"state\":\"ON\",\"brightness\":255}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":1750,"y":1840,"wires":[["16f771ab.f7b76e"]]},{"id":"3326c0b8.9b6e9","type":"change","z":"d11dfeaa.8b609","name":"Zet uit","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"state\":\"ON\",\"brightness\":0,\"transition\":20}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":1730,"y":2000,"wires":[["16f771ab.f7b76e"]]},{"id":"149746b3.fa4749","type":"switch","z":"d11dfeaa.8b609","name":"","property":"payload.occupancy","propertyType":"msg","rules":[{"t":"cont","v":"true","vt":"str"},{"t":"cont","v":"false","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":1570,"y":1860,"wires":[["7e36cfcf.49ce3"],["3326c0b8.9b6e9"]]},{"id":"62f2f1cf.96447","type":"switch","z":"d11dfeaa.8b609","name":"Lux","property":"payload.illuminance","propertyType":"msg","rules":[{"t":"gt","v":"20","vt":"str"},{"t":"btwn","v":"11","vt":"str","v2":"20","v2t":"str"},{"t":"btwn","v":"5","vt":"str","v2":"10","v2t":"str"},{"t":"lt","v":"5","vt":"str"}],"checkall":"false","repair":false,"outputs":4,"x":1370,"y":2020,"wires":[["3326c0b8.9b6e9"],["e50de4f.eb16218"],["fd05f68f.2e4998"],["149746b3.fa4749"]]},{"id":"d862fdf2.b403a","type":"change","z":"d11dfeaa.8b609","name":"Zet aan (150)","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"state\":\"ON\",\"brightness\":150}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":1750,"y":1900,"wires":[["16f771ab.f7b76e"]]},{"id":"fd05f68f.2e4998","type":"switch","z":"d11dfeaa.8b609","name":"","property":"payload.occupancy","propertyType":"msg","rules":[{"t":"cont","v":"true","vt":"str"},{"t":"cont","v":"false","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":1570,"y":1920,"wires":[["d862fdf2.b403a"],["3326c0b8.9b6e9"]]},{"id":"d4298fe2.c9029","type":"change","z":"d11dfeaa.8b609","name":"Zet aan (100)","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"state\":\"ON\",\"brightness\":100}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":1750,"y":1960,"wires":[["16f771ab.f7b76e"]]},{"id":"e50de4f.eb16218","type":"switch","z":"d11dfeaa.8b609","name":"","property":"payload.occupancy","propertyType":"msg","rules":[{"t":"cont","v":"true","vt":"str"},{"t":"cont","v":"false","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":1570,"y":1980,"wires":[["d4298fe2.c9029"],["3326c0b8.9b6e9"]]},{"id":"d3252612.e50fd8","type":"time-range-switch","z":"d11dfeaa.8b609","name":"","lat":"","lon":"","startTime":"sunrise","endTime":"sunset","startOffset":0,"endOffset":"-60","x":1240,"y":1940,"wires":[["5e1a1122.9ae57"],["149746b3.fa4749"]]},{"id":"1dc32f82.4faf7","type":"json","z":"d11dfeaa.8b609","name":"","property":"payload","action":"","pretty":false,"x":1270,"y":1880,"wires":[["d3252612.e50fd8"]]},{"id":"5e1a1122.9ae57","type":"rbe","z":"d11dfeaa.8b609","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload.occupancy","x":1230,"y":2020,"wires":[["62f2f1cf.96447"]]},{"id":"7a291092.162ac","type":"mqtt-broker","z":"","name":"MQTT","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

It switches on/off the light in a room by motion. I use the xiaomi human body sensor and an ikea light with zigbee2mqtt to achieve this.

What I want to achieve now is, that when I turn the power off and than back on, within a few seconds, that this will override the motion flow described above. The goal is to keep the light on. And when I do this again, it will return back to the original motion flow.

When I use the original hardwired switch on the wall, it will create a message to: zigbee2mqtt/Lamp_bijkamer/availability. It can be "offline" or "online", depending if the power is switched off or on. So I need to use these messages. But I can't figure out how to do this.

Does anyone now how to do this?

This is my 10th effort at answering your post and suddenly the solution is obvious! You are using MQTT to communicate between your devices and describe state changes and that is the key and is why MQTT is used for these jobs. I can't fix your problem quickly because your current setup is complex already but I can give you a strategy:

  • Take a piece of paper and a pen or pencil
  • Draw a simple flow chart of what you want to happen
  • Create a code to describe what messages cause which states
  • Work out how to send and receive those codes via MQTT
  • Convert the flow chart into NR nodes and messages

Profit!

You need to think about your various inputs and outputs. Some switches should "know" when the state has changed and some do not need that knowledge. For example: a physical switch (a wall switch) can only be on or off when it is physically moved from one state to the other. You may have to change the state of other switches because a physical one has been changed. You could also consider what happens when the power fails.

Control systems are difficult!

Cheers
Jon

PS I wish you all the best and have discovered what it is like trying to deal with a foreign language in a pretty complex IT puzzle. I've tried to simplify my English enough but not too much.

I have reworked your flow a bit. The lux value is now variable, based on illuminance with a transistion.

To capture the wall switch, a global variable will be set (upon restart of flow/node-red default to false).
If switch = true, the flow will be ignored and the light remains on.

Obviously I cannot test your setup, but this is the general idea. Hope it gives you some ideas.

[{"id":"bb6e8073.33477","type":"mqtt in","z":"8a56af38.48a908","name":"Motion bijkamer","topic":"zigbee2mqtt/Bewegingsmelder_bijkamer","qos":"0","datatype":"json","broker":"28450957.9522ae","x":102,"y":88,"wires":[["ea4b2d64.03f038"]]},{"id":"7dcba731.0917d8","type":"mqtt out","z":"8a56af38.48a908","name":"Bijkamer lamp","topic":"zigbee2mqtt/Lamp_bijkamer/set","qos":"0","retain":"false","broker":"28450957.9522ae","x":1092,"y":330,"wires":[]},{"id":"ea4b2d64.03f038","type":"time-range-switch","z":"8a56af38.48a908","name":"","lat":"","lon":"","startTime":"sunrise","endTime":"sunset","startOffset":0,"endOffset":"-60","x":278,"y":88,"wires":[["e2712232.b795e"],[]]},{"id":"e2712232.b795e","type":"rbe","z":"8a56af38.48a908","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload.occupancy","x":336,"y":154,"wires":[["ae60a1af.0fbb78"]]},{"id":"fa84e986.f9e04","type":"range","z":"8a56af38.48a908","minin":"0","maxin":"19","minout":"0","maxout":"255","action":"scale","round":false,"property":"payload.illuminance","name":"","x":610,"y":242,"wires":[["d346347b.16115"]]},{"id":"d346347b.16115","type":"change","z":"8a56af38.48a908","name":"aan","rules":[{"t":"set","p":"payload.state","pt":"msg","to":"ON","tot":"str"},{"t":"set","p":"payload.brightness","pt":"msg","to":"payload.illuminance","tot":"msg"},{"t":"set","p":"payload.transition","pt":"msg","to":"5","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":754,"y":242,"wires":[["fc84f5cd.487c08"]]},{"id":"ae60a1af.0fbb78","type":"switch","z":"8a56af38.48a908","name":"","property":"payload.illuminance","propertyType":"msg","rules":[{"t":"lt","v":"20","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":264,"y":264,"wires":[["e85cd3d.109e33"],["2e7854c.101b52c"]]},{"id":"2e7854c.101b52c","type":"change","z":"8a56af38.48a908","name":"uit","rules":[{"t":"set","p":"payload.state","pt":"msg","to":"OFF","tot":"str"},{"t":"set","p":"payload.brightness","pt":"msg","to":"0","tot":"num"},{"t":"set","p":"payload.transition","pt":"msg","to":"20","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":286,"wires":[["fc84f5cd.487c08"]]},{"id":"8b74421.16549c","type":"switch","z":"8a56af38.48a908","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":270,"y":396,"wires":[["5845f9e6.e4f7e"],["437410fd.846eb"]]},{"id":"5845f9e6.e4f7e","type":"change","z":"8a56af38.48a908","name":"","rules":[{"t":"set","p":"Lamp_bijkamer","pt":"global","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":462,"y":374,"wires":[["e113e245.fca1e8"]]},{"id":"437410fd.846eb","type":"change","z":"8a56af38.48a908","name":"","rules":[{"t":"set","p":"Lamp_bijkamer","pt":"global","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":462,"y":418,"wires":[["4c4ed085.11a4a8"]]},{"id":"fc84f5cd.487c08","type":"switch","z":"8a56af38.48a908","name":"Handmatig aan ?","property":"Lamp_bijkamer","propertyType":"global","rules":[{"t":"false"},{"t":"true"}],"checkall":"true","repair":false,"outputs":2,"x":904,"y":286,"wires":[["7dcba731.0917d8"],[]]},{"id":"2b9fcdb8.3868b2","type":"inject","z":"8a56af38.48a908","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":94,"y":550,"wires":[["391907d1.cddac8"]]},{"id":"391907d1.cddac8","type":"change","z":"8a56af38.48a908","name":"","rules":[{"t":"set","p":"Lamp_bijkamer","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":286,"y":550,"wires":[[]]},{"id":"5360fde7.469004","type":"comment","z":"8a56af38.48a908","name":"global false bij deploy/startup","info":"","x":142,"y":506,"wires":[]},{"id":"e113e245.fca1e8","type":"change","z":"8a56af38.48a908","name":"aan","rules":[{"t":"set","p":"payload.state","pt":"msg","to":"ON","tot":"str"},{"t":"set","p":"payload.brightness","pt":"msg","to":"255","tot":"num"},{"t":"set","p":"payload.transition","pt":"msg","to":"20","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":374,"wires":[["7dcba731.0917d8"]]},{"id":"4c4ed085.11a4a8","type":"change","z":"8a56af38.48a908","name":"uit","rules":[{"t":"set","p":"payload.state","pt":"msg","to":"OFF","tot":"str"},{"t":"set","p":"payload.brightness","pt":"msg","to":"0","tot":"num"},{"t":"set","p":"payload.transition","pt":"msg","to":"20","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":418,"wires":[["7dcba731.0917d8"]]},{"id":"d180b093.59f068","type":"mqtt in","z":"8a56af38.48a908","name":"Switch handmatig","topic":"zigbee2mqtt/Lamp_bijkamer/availability","qos":"0","datatype":"auto","broker":"28450957.9522ae","x":112,"y":396,"wires":[["8b74421.16549c"]]},{"id":"e85cd3d.109e33","type":"switch","z":"8a56af38.48a908","name":"Occupancy ?","property":"payload.occupancy","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":422,"y":242,"wires":[["fa84e986.f9e04"],["2e7854c.101b52c"]]},{"id":"28450957.9522ae","type":"mqtt-broker","z":"","name":"MQTT","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

The flow is not working, but it gives me a different point of view on the matter. So I will investigate further and report back.