Nodered noob question

Very naive question.
I need to set an attribute (occupancy:true) of motion sensor using a function node that inject a msg to a zigbee2mqtt-out node.

my funcion code is:

msg1={payload:{"occupancy":true}};

if (msg.payload.action=="on"){
    return [msg1];    
}

the function correctly send the message.
The receiving node does’nt perform action (the occupancy value remain “clear” in z2m). I think it is a question of node configuration. It seems trivial. It is trivial, but I’m getting mad.
Can someone post an image of the correct node configuration?

If i understand correctly you are trying to manually set a devices occupancy status to true, which you simply cannot do. Devices do not accept commands to change sensor states, only to report them and adjust settings.

The out node is meant for doing things like turning switches on and off, setting light brightness or color etc.

It sounds more like you need a variable to trigger against, or a virtual device, versus a real device for whatever your trying to do.

1 Like

Could you please explain what you’re trying to do? We can come up with a thousand ways to accomplish something. And they’re all correct.

1 Like

Ok. That make sense, although to force a sensor state could be usefull.
I needed that capability in my multisensor motion ligth automation.
I have a inwall zigbee light switch. It send a mqtt msg when I use the phisical switch.
When I exit room, I would like to manually switch off forcing the lght to remain off. Actually my automation arise the light up until the sensors are all with occupancy=false.
The only solution I can imagine is a flow variable that disable for a while the lights. This solution has some othe drawback I need to solve.
Thnak you @RRodman

Ok after reading that a couple times, You need the switch being physically turned off to turn off the lights regardless of the what anything else is doing, and to stay off until the switch is physically turned on again?

If that is the case have the switch status go into a variable, and when motion is triggered check against that variable. If the variable isn’t true/on dont go any further, if it is true/on allow the flow to continue and turn on the lights. Have the switch being pressed off bypass all checks and timers and turn off the lights.

If you just want the physical switch to over-ride the motion sensors when turned off, but still want the motion sensors to turn the light on without the switch being touched, make sure all of your z2m in nodes ONLY have the needed attribute selected, and are also set to only report when value changes.

Then you can simply tie the switch being set to off to the rooms off command. bypassing all the timers and checks. However you will need the switch to NOT have only send on change checked, or it will only work when you have physically turned the switch on before turning it off.

If thats not what your trying to do, then I’m sorry but I’m not understanding and hopefully someone else can fill in the gaps/point you in the right direction.

To set the switch status from node red make sure you pay attention to the out node settings, including the payload format its looking for the commands on.


And that your message is addressing it properly

That is partially true. The disable state need to last until a person enter the room again (what is now happening is that the ligth after few seconds from physical switch off suddnly switch on again until no movements are detected within the implemented time window)
I think I take another path that should work. Since to turn on the light I test the global variables that represent the last state of the sensors, when I press the button instead of trying to change the status of the sensors, I simply modify the global variables preventing the flow from traveling the branch that leads to the ignition of the light. At very first tempative it works. Deeper testing is necessary.

@april.brandt April is right. As soon as you stop insisting on an impossible solution, another solution suddenly appears!

1 Like

The challenge is finding that way that makes sense to you. That “AHA!” Moment.

It makes me reflect on the fact that I once struggled to make sense of these flows and what they do. How do I make this work? Just don’t forget that debug nodes will shed light on things that you found … challenging … to answer.

Post your final findings so that others can learn and see how you solved your situation.

1 Like

Purpose of the flow
This flow manages the switching of one light in a room through the use of multiple sensors and also optionally allows, when you leave the room, the usage of a physical button to avoid waiting for the sensors to switch off the light .
The motion sensors (in this example there are three but you can be increase them) are divided into “trigger” sensors, that is, those that, if simultaneously active, turn on the light and “maintain” sensors, that is, those that, if active, only prevent the light from turning off .

How it works
The top of the flow represents a typical implementation of a multisensor motion light app.
Not being able to act on the occupancy status of the sensors to deactivate them when the physical button of the switch is pressed, I decided to store the status of the sensors and of the switch in global variables of the flow type using those variables in the flow logic. Switching off via the switch button simply clears the variables.
The user, after having switched off the light with the physical button, has 3 seconds to exit the detection zone of the trigger sensors and therefore prevent the light from switching on again due to the implemented automation.

Points of attention
The implementation of the physical button management is dependent on the user legacy switch. I used a bticino zigbee inwall switch, but the underlying logic should also apply to other brands of switches, as long as they send a notification message if the physical button is used.

[{"id":"7fbbdba721a89b97","type":"tab","label":"CUCINA","disabled":false,"info":"","env":[]},{"id":"8620dc0d8e48a371","type":"group","z":"7fbbdba721a89b97","name":"TRIGGER SENSORS","style":{"label":true},"nodes":["49d68c3813d21451","f2e80f0725395bb8","b5b3ad1c197baf30","6e018b218495f9ee","51968669ebef358d","40759a3f13e8fe06"],"x":74,"y":79,"w":592,"h":122},{"id":"8d37ea1c4d078adf","type":"group","z":"7fbbdba721a89b97","name":"OTHER MOTION SENSORS TO KEEP ","style":{"label":true},"nodes":["4d660266de9b78e0","74b66901806ca4ef","b6b4a396e0b772f7"],"x":74,"y":219,"w":592,"h":82},{"id":"d98c385ff654245a","type":"group","z":"7fbbdba721a89b97","name":"PHYSICAL BUTTON MANAGEMENT","style":{"label":true},"nodes":["c90b36c3d663ef7f","ea64e6b165473af4","0602fa6f2943036d","20d50f7f1d5b50d4","cc1d12dcc73ef143","14c798dea7e7e233","f7f16a90f3ff98af","bbac0bf76575a6ec"],"x":74,"y":359,"w":1052,"h":202},{"id":"a4f62693605d3f46","type":"stoptimer2","z":"7fbbdba721a89b97","duration":"60","durationType":"num","units":"Second","payloadtype":"num","payloadval":"0","name":"45","x":1430,"y":240,"wires":[["34de61f10d1f2d4b"],[]]},{"id":"ebb8697785586fa4","type":"switch","z":"7fbbdba721a89b97","name":"MT09","property":"#:(ram)::MT09.occupancy","propertyType":"flow","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":950,"y":240,"wires":[["d02c49c182a2ff15"]]},{"id":"d02c49c182a2ff15","type":"switch","z":"7fbbdba721a89b97","name":"MT08","property":"#:(ram)::MT08.occupancy","propertyType":"flow","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":1090,"y":240,"wires":[["e07349ea7dcb8a64"]]},{"id":"15ed7a39e934c805","type":"change","z":"7fbbdba721a89b97","name":"STOP  ","rules":[{"t":"set","p":"payload","pt":"msg","to":"STOP","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1250,"y":160,"wires":[["a4f62693605d3f46"]]},{"id":"49d68c3813d21451","type":"change","z":"7fbbdba721a89b97","g":"8620dc0d8e48a371","name":"set flow.MT09","rules":[{"t":"set","p":"#:(ram)::MT09","pt":"flow","to":"payload","tot":"msg","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":120,"wires":[["62523b067ec79220"]]},{"id":"f2e80f0725395bb8","type":"change","z":"7fbbdba721a89b97","g":"8620dc0d8e48a371","name":"set flow.MT08","rules":[{"t":"set","p":"#:(ram)::MT08","pt":"flow","to":"payload","tot":"msg","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":160,"wires":[["62523b067ec79220"]]},{"id":"e07349ea7dcb8a64","type":"change","z":"7fbbdba721a89b97","name":"START","rules":[{"t":"set","p":"payload","pt":"msg","to":"START","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1240,"y":240,"wires":[["a4f62693605d3f46"]]},{"id":"1a42ca03847b6f72","type":"change","z":"7fbbdba721a89b97","name":"On","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"state\":\"on\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":1570,"y":140,"wires":[["f664f8a4ba9feb1f"]]},{"id":"34de61f10d1f2d4b","type":"change","z":"7fbbdba721a89b97","name":"Off","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"state\":\"off\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":1570,"y":180,"wires":[["f664f8a4ba9feb1f"]]},{"id":"84f94a2842617025","type":"comment","z":"7fbbdba721a89b97","name":"Get sensor occupancy value, if it changed store new status. ----------------------------------------------- Check occupancy status","info":"","x":480,"y":40,"wires":[]},{"id":"75e1c42c6eaf158b","type":"comment","z":"7fbbdba721a89b97","name":"NO: If all sensors are false, start delay timer before to switch off","info":"","x":1130,"y":200,"wires":[]},{"id":"3d0a636787c908e4","type":"comment","z":"7fbbdba721a89b97","name":"Send ON/OFF commands","info":"","x":1630,"y":80,"wires":[]},{"id":"4d660266de9b78e0","type":"change","z":"7fbbdba721a89b97","g":"8d37ea1c4d078adf","name":"set flow.MT07","rules":[{"t":"set","p":"#:(ram)::MT07","pt":"flow","to":"payload","tot":"msg","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":260,"wires":[["62523b067ec79220"]]},{"id":"eeb0ae772ade2e13","type":"switch","z":"7fbbdba721a89b97","name":"MT09","property":"#:(ram)::MT09.occupancy","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":950,"y":120,"wires":[["67b8d7abb150f9a0"]]},{"id":"67b8d7abb150f9a0","type":"switch","z":"7fbbdba721a89b97","name":"MT08","property":"#:(ram)::MT08.occupancy","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":1090,"y":120,"wires":[["0dddb466211984ac"]]},{"id":"a9bce4c7075ac5db","type":"comment","z":"7fbbdba721a89b97","name":"YES: stop pending timer if any, if all trigger sensors are true, switch on","info":" 1. ","x":1150,"y":80,"wires":[]},{"id":"b5b3ad1c197baf30","type":"zigbee2mqtt-in","z":"7fbbdba721a89b97","g":"8620dc0d8e48a371","name":"MT09-CUCINA-MURO","server":"411e2b8e9bdac70b","friendly_name":"MT09-CUCINA-MURO (SNZB-03)","device_id":"0x00124b001f90d0ed","state":"0","outputAtStartup":true,"filterChanges":false,"enableMultiple":false,"x":200,"y":120,"wires":[["51968669ebef358d"]]},{"id":"6e018b218495f9ee","type":"zigbee2mqtt-in","z":"7fbbdba721a89b97","g":"8620dc0d8e48a371","name":"MT08-CUCINA-CAPPA","server":"411e2b8e9bdac70b","friendly_name":"MT08-CUCINA-CAPPA (9290030675)","device_id":"0x001788010bcfe767","state":"0","outputAtStartup":true,"filterChanges":false,"enableMultiple":false,"x":200,"y":160,"wires":[["40759a3f13e8fe06"]]},{"id":"74b66901806ca4ef","type":"zigbee2mqtt-in","z":"7fbbdba721a89b97","g":"8d37ea1c4d078adf","name":"MT07-CUCINA-TAVOLO","server":"411e2b8e9bdac70b","friendly_name":"MT07-CUCINA-TAVOLO (SNZB-03)","device_id":"0x00124b001f90d701","state":"0","outputAtStartup":true,"filterChanges":false,"enableMultiple":false,"x":210,"y":260,"wires":[["b6b4a396e0b772f7"]]},{"id":"62523b067ec79220","type":"switch","z":"7fbbdba721a89b97","name":"occupied?","property":"payload.occupancy","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":790,"y":180,"wires":[["15ed7a39e934c805","eeb0ae772ade2e13"],["ebb8697785586fa4"]]},{"id":"0dddb466211984ac","type":"switch","z":"7fbbdba721a89b97","name":"<= LUX","property":"#:(ram)::MT08.illuminance_lux","propertyType":"flow","rules":[{"t":"lt","v":"55","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1240,"y":120,"wires":[["1a42ca03847b6f72"]]},{"id":"f664f8a4ba9feb1f","type":"zigbee2mqtt-out","z":"7fbbdba721a89b97","name":"SW05-CUCINA","server":"411e2b8e9bdac70b","friendly_name":"SW05-CUCINA (K4003C/L4003C/N4003C/NT4003C)","device_id":"0x000474000113e511","command":"state","commandType":"z2m_cmd","payload":"payload","payloadType":"msg","optionsValue":"","optionsType":"nothing","x":1720,"y":160,"wires":[]},{"id":"c90b36c3d663ef7f","type":"zigbee2mqtt-in","z":"7fbbdba721a89b97","g":"d98c385ff654245a","name":"SW05-CUCINA","server":"411e2b8e9bdac70b","friendly_name":"SW05-CUCINA (K4003C/L4003C/N4003C/NT4003C)","device_id":"0x000474000113e511","state":"0","outputAtStartup":true,"filterChanges":true,"enableMultiple":false,"x":180,"y":480,"wires":[["0602fa6f2943036d"]]},{"id":"ea64e6b165473af4","type":"comment","z":"7fbbdba721a89b97","g":"d98c385ff654245a","name":"Check if physical button was pushed ","info":"","x":240,"y":400,"wires":[]},{"id":"0602fa6f2943036d","type":"switch","z":"7fbbdba721a89b97","g":"d98c385ff654245a","name":"physical button?","property":"payload.action","propertyType":"msg","rules":[{"t":"eq","v":"identify","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":360,"y":480,"wires":[["14c798dea7e7e233"],["20d50f7f1d5b50d4"]]},{"id":"20d50f7f1d5b50d4","type":"change","z":"7fbbdba721a89b97","g":"d98c385ff654245a","name":"","rules":[{"t":"set","p":"#:(ram)::SW05","pt":"flow","to":"payload","tot":"msg","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":520,"wires":[[]]},{"id":"cc1d12dcc73ef143","type":"function","z":"7fbbdba721a89b97","g":"d98c385ff654245a","name":"delete flow.MT*","func":"flow.set(\"MT09\");\nflow.set(\"MT08\");\nflow.set(\"MT07\");\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":740,"y":440,"wires":[[]]},{"id":"14c798dea7e7e233","type":"stoptimer2","z":"7fbbdba721a89b97","g":"d98c385ff654245a","duration":"3","durationType":"num","units":"Second","payloadtype":"num","payloadval":"0","name":"","x":560,"y":440,"wires":[["cc1d12dcc73ef143"],[]]},{"id":"51968669ebef358d","type":"switch","z":"7fbbdba721a89b97","g":"8620dc0d8e48a371","name":"changed?","property":"payload.occupancy","propertyType":"msg","rules":[{"t":"neq","v":"#:(ram)::MT09.ccupancy","vt":"flow"}],"checkall":"true","repair":false,"outputs":1,"x":400,"y":120,"wires":[["49d68c3813d21451"]]},{"id":"40759a3f13e8fe06","type":"switch","z":"7fbbdba721a89b97","g":"8620dc0d8e48a371","name":"changed?","property":"payload.occupancy","propertyType":"msg","rules":[{"t":"neq","v":"#:(ram)::MT08.occupancy","vt":"flow"}],"checkall":"true","repair":false,"outputs":1,"x":400,"y":160,"wires":[["f2e80f0725395bb8"]]},{"id":"b6b4a396e0b772f7","type":"switch","z":"7fbbdba721a89b97","g":"8d37ea1c4d078adf","name":"changed?","property":"payload.occupancy","propertyType":"msg","rules":[{"t":"neq","v":"#:(ram)::MT07.occupancy","vt":"flow"}],"checkall":"true","repair":false,"outputs":1,"x":400,"y":260,"wires":[["4d660266de9b78e0"]]},{"id":"f7f16a90f3ff98af","type":"comment","z":"7fbbdba721a89b97","g":"d98c385ff654245a","name":"YES: delay few seconds (to avoid sensor rebouncing), and delete all motion flow variables ","info":"","x":790,"y":400,"wires":[]},{"id":"bbac0bf76575a6ec","type":"comment","z":"7fbbdba721a89b97","g":"d98c385ff654245a","name":"NO: store status of the switch","info":"","x":600,"y":480,"wires":[]},{"id":"411e2b8e9bdac70b","type":"zigbee2mqtt-server","name":"CORE","host":"10.10.2.1","mqtt_port":"1883","mqtt_username":"","mqtt_password":"","mqtt_qos":"0","tls":"","usetls":false,"base_topic":"zigbee2mqtt"}]

The comments inside the flow should help to understand the internal logic.

Curiosities
The “in line” check of the occupancy state of the trigger sensors is the equivalent of an AND.
If you would like to make an equivalent of the HE zones, you could insert also the maintain nodes.
oh-la 1

I posted my solution

1 Like

This comment is so, so true.
As a relative noob to NR, when I build a ‘new’ flow (not a replica of an existing one) I follow the flow through with a debug node. This confirms what I am expecting to see to be true. On numerous occasions I’ve assumed something will be generated in a flow and it hasn’t. A debug node reveals all. It also can stop a lot of head scratching.

2 Likes

Gosh. I removed the debug nodes just to let the diagram be more readable… :- :shushing_face:

2 Likes

I remove mine too when I have a flow working as I want. :+1:

I do like the look of your flow above. Some good ideas in there that I could plagiarise. :wink:

1 Like