Problem with current state

I'm making a simple scheme that the light doesn't turn off as soon as the motion sensor stops detecting motion.
The problem is in the current state node "if lamp is OFF". If it is triggered by a timestamp1, then everything works as it should: If the lamp is on, then processing is transferred to the "trigger 30s". But if this node is triggered by "motion sensor ON" and the lamp is also turned on, then processing is transferred to "lamp turn ON".
Why?
Maybe the whole scheme is wrong?

[{"id":"33b6ff569cc20110","type":"tab","label":"Поток тест","disabled":false,"info":"","env":[]},{"id":"8b2566e66474e64a","type":"api-call-service","z":"33b6ff569cc20110","name":"lamp turn ON","server":"9055ffae.cacf6","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lobby","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":710,"y":140,"wires":[[]]},{"id":"d1b0d90ea5dbedcb","type":"api-call-service","z":"33b6ff569cc20110","name":"lamp turn OFF","server":"9055ffae.cacf6","version":3,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.lobby","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":740,"y":220,"wires":[[]]},{"id":"13d7b0ce0f759053","type":"ha-device","z":"33b6ff569cc20110","name":"motion sensor ON","server":"9055ffae.cacf6","version":0,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"inputs":0,"deviceType":"trigger","device":"f22f6ab8ee392b66d2bd80f2ffb2b5de","event":{"type":"motion","platform":"device","device_id":"f22f6ab8ee392b66d2bd80f2ffb2b5de","entity_id":"binary_sensor.0x00158d0000b3b872_occupancy","domain":"binary_sensor"},"capabilities":[],"outputProperties":[{"property":"payload","propertyType":"msg","value":"off","valueType":"str"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":150,"y":140,"wires":[["1e77671f99909a74"]]},{"id":"affb3c5867f4721e","type":"ha-device","z":"33b6ff569cc20110","name":"motion sensor OFF","server":"9055ffae.cacf6","version":0,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"inputs":0,"deviceType":"trigger","device":"f22f6ab8ee392b66d2bd80f2ffb2b5de","event":{"type":"no_motion","platform":"device","device_id":"f22f6ab8ee392b66d2bd80f2ffb2b5de","entity_id":"binary_sensor.0x00158d0000b3b872_occupancy","domain":"binary_sensor"},"capabilities":[],"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":150,"y":220,"wires":[["0c264fd372109895"]]},{"id":"0c264fd372109895","type":"trigger","z":"33b6ff569cc20110","name":"","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"30","extend":false,"overrideDelay":false,"units":"s","reset":"off","bytopic":"all","topic":"topic","outputs":1,"x":430,"y":220,"wires":[["d1b0d90ea5dbedcb"]]},{"id":"1e77671f99909a74","type":"api-current-state","z":"33b6ff569cc20110","name":"if lamp is OFF","server":"9055ffae.cacf6","version":2,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.lobby","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"off","valueType":"str"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":420,"y":80,"wires":[["8b2566e66474e64a"],["0c264fd372109895"]]},{"id":"838f4e2e1f5342de","type":"inject","z":"33b6ff569cc20110","name":"timestamp1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":130,"y":80,"wires":[["1e77671f99909a74"]]},{"id":"ae1b79bb8dbd01bf","type":"inject","z":"33b6ff569cc20110","name":"timestamp2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":150,"y":280,"wires":[["0c264fd372109895"]]},{"id":"9055ffae.cacf6","type":"server","name":"","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":""}]

image

This will make helping you a bit difficult.

What does the debug say when you attach it to the nodes?

Thanks. Which nodes do you recommend attaching the debug to?

From timestamp1 " msg.payload : number
1645200195384"
from motion detection "msg.payload : string[3]
"off""
Does it matter what message comes to the "current state" node?

all

In my opinion, the more info the better when struggling with something.

That is why I am fond of these:

[{"id":"544a325ff2a412cb","type":"display property","z":"3ab61f50.f9635","name":"Display msg.topic","property":"msg.topic","showDate":0,"showTime":0,"x":830,"y":2180,"wires":[["645bc627141d09bd"]]},{"id":"e365ba111b38c120","type":"display property","z":"3ab61f50.f9635","name":"Display msg.payload","property":"Display msg.payload","showDate":0,"showTime":0,"x":840,"y":2260,"wires":[["645bc627141d09bd"]]},{"id":"3126bd0e3529dc53","type":"display property","z":"3ab61f50.f9635","name":"Display msg.object","property":"Display msg.object","showDate":0,"showTime":0,"x":830,"y":2340,"wires":[["645bc627141d09bd"]]},{"id":"33ba34918f0d1376","type":"inject","z":"3ab61f50.f9635","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":560,"y":2260,"wires":[["544a325ff2a412cb","e365ba111b38c120","3126bd0e3529dc53"]]},{"id":"645bc627141d09bd","type":"switch","z":"3ab61f50.f9635","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1150,"y":2260,"wires":[[]]}]

stick these between all links and you might find some info

once solved, remove some property nodes :slight_smile:

Does it matter what message comes to the "current state" node?

Depends on the way the nodes are set up... I can't see them since I don't have the nodes. do you have a link so I can install them?

library: node-red-contrib-home-assistant-websocket

Added a debug node and now everything works as it should. Mystic!

Well it was not the debug node that fixed it.

2 Likes

I don't have an answer to your problem (nor the mystic that solved it :wink: ), but I think you could build this somewhat simpler if your motion sensor works the same as mine (Xiaomi). I just filter for occupied messages and if I get one, send it to a trigger node. The first message of the trigger node sends an on command and if within the next 2 minutes another occupied message comes in, the time is extended (standard behaviour) and if not, after 2 minutes the off command is sent. I think that solution is nice, simple and foolproof.
image

Thanks. It seems to me that the motion sensor works in a different way. It gives two messages - 1 "detecting motion" and 2 "stopped detecting motion".
If the movement continues, the message 1 will arrive only once and will not be repeated. The light will turn off after 2 minutes. Is it?

Btw, this behavior is what I also observe for the hue motion sensors. Took me a while to find that out.

yeah, indeed, without the occupied message repeating, the light would turn off after 2 minutes

Nevertheless, I solved that a bit differently. Still based on the trigger node. Will show you my solution later when again being at home (in a few hours).

But the occupied message will only be repeated if there is a break in moving. The "stop detecting occupied" message will appear first, then the occupied message will repeat. If the movement is continuous without breaking, the message will not be repeated.

I would not rely on there being no repeated 'occupied' messages, or indeed repeated 'unnocupied' messages. Your flow should cope with that.

1 Like

Certainly. I just wanted to say that the flow by deputynl will not work correctly. Does anyone know a working solution?

Can you explain again what you are trying to achieve, I have lost track of this along the way.

Also remind me what the incoming payload looks like and what output you want for On and Off.

There is a motion sensor and a lamp. Normally, the motion sensor turns off the lamp after 1 min without detecting motion. I just want to increase this period.

And the payload values?

payload values ​​from motion sensor i can do anything

The trick is firstly to use a Switch node to separate motion start and stop. Pass the start message on directly to switch the light on, send the Off message to a Trigger node that waits for the time required and then switches the light off. To stop the light being turned off if the motion starts again before the trigger has timed out, also feed the start message to the trigger, but configure the 'Reset Trigger If' option in the Trigger node to reset if the payload is equal to the start motion payload, so that it resets the trigger and it does not send the light off command. I have set the trigger to 10 seconds in the demo flow to make it easier to test.

[{"id":"1511d45dd7baee43","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":110,"y":1260,"wires":[["d09f74b5913ecad9"]]},{"id":"583a7755e99f5859","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":110,"y":1320,"wires":[["d09f74b5913ecad9"]]},{"id":"dded18b24cc7b073","type":"change","z":"bdd7be38.d3b55","name":"Lamp on","rules":[{"t":"set","p":"payload","pt":"msg","to":"On","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":1280,"wires":[["8899f8e7fdb89ad6"]]},{"id":"d09f74b5913ecad9","type":"switch","z":"bdd7be38.d3b55","name":"Occupied?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":270,"y":1300,"wires":[["86113191cf89969d","dded18b24cc7b073"],["86113191cf89969d"]]},{"id":"86113191cf89969d","type":"trigger","z":"bdd7be38.d3b55","name":"","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"10","extend":false,"overrideDelay":false,"units":"s","reset":"1","bytopic":"all","topic":"topic","outputs":1,"x":470,"y":1320,"wires":[["7bdb253098f142aa"]]},{"id":"7bdb253098f142aa","type":"change","z":"bdd7be38.d3b55","name":"Lamp off","rules":[{"t":"set","p":"payload","pt":"msg","to":"Off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":1320,"wires":[["8899f8e7fdb89ad6"]]},{"id":"8899f8e7fdb89ad6","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":1300,"wires":[]}]