# Problem with current state

**URL:** <https://discourse.nodered.org/t/problem-with-current-state/58599>\
**Category:** General\
**Created:** [18 February 2022 12:55 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599 "2022-02-18T12:55:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [18 February 2022 12:55 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/1 "2022-02-18T12:55:33Z")

</div>

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?

```auto
[{"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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/e/5e094a2e50bf1b95ea7f5db8d3e817555ee22632.png)

---

<div class="post-metadata">

**Author:** ![WBr](https://avatars.discourse-cdn.com/v4/letter/w/b3f665/32.png) [@WBr](https://discourse.nodered.org/u/WBr)\
**Post date:** [18 February 2022 15:14 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/2 "2022-02-18T15:14:44Z")

</div>

> [@Leopoll](#):
>
> turn off as soon as the motion sensor stops detecting motion.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/c/5c1686247548334df6a5a3441ff3e0da7dfb37bf.png)

This will make helping you a bit difficult.

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

---

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [18 February 2022 15:47 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/3 "2022-02-18T15:47:21Z")

</div>

> [@WBr](#):
>
> What does the debug say when you attach it to the nodes?

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

---

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [18 February 2022 16:12 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/4 "2022-02-18T16:12:06Z")

</div>

> [@WBr](#):
>
> What does the debug say when you attach it to the nodes?

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?

---

<div class="post-metadata">

**Author:** ![WBr](https://avatars.discourse-cdn.com/v4/letter/w/b3f665/32.png) [@WBr](https://discourse.nodered.org/u/WBr)\
**Post date:** [18 February 2022 16:16 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/5 "2022-02-18T16:16:08Z")

</div>

all

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

That is why I am fond of these:

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

```

> **[node-red-contrib-display-property](https://flows.nodered.org/node/node-red-contrib-display-property)**
>
> A simple node that displays as its status the value of a message property that passes the node and optional the date/time when the message passed.

stick these between all links and you might find some info

once solved, remove some property nodes 🙂

> 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?

---

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [18 February 2022 19:31 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/6 "2022-02-18T19:31:48Z")

</div>

> [@WBr](#):
>
> 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!

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b2cdcf2759f56a9c02fcb3010ced68abe10c57c7.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 February 2022 21:07 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/7 "2022-02-18T21:07:45Z")

</div>

Well it was not the debug node that fixed it.

---

<div class="post-metadata">

**Author:** ![deputynl](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@deputynl](https://discourse.nodered.org/u/deputynl)\
**Post date:** [21 February 2022 12:29 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/8 "2022-02-21T12:29:19Z")

</div>

I don't have an answer to your problem (nor the mystic that solved it 😉 ), 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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b51254c600b58e4d764fe706ce71891156425b60.png)

---

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [23 February 2022 14:32 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/9 "2022-02-23T14:32:37Z")

</div>

> [@deputynl](#):
>
> 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

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?

---

<div class="post-metadata">

**Author:** ![ceysa75](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ceysa75/32/31340_2.png) [@ceysa75](https://discourse.nodered.org/u/ceysa75)\
**Post date:** [23 February 2022 14:50 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/10 "2022-02-23T14:50:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![deputynl](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@deputynl](https://discourse.nodered.org/u/deputynl)\
**Post date:** [23 February 2022 14:52 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/11 "2022-02-23T14:52:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ceysa75](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ceysa75/32/31340_2.png) [@ceysa75](https://discourse.nodered.org/u/ceysa75)\
**Post date:** [23 February 2022 14:55 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/12 "2022-02-23T14:55:29Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [23 February 2022 15:39 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/13 "2022-02-23T15:39:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 February 2022 15:53 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/14 "2022-02-23T15:53:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [23 February 2022 16:35 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/15 "2022-02-23T16:35:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 February 2022 16:43 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/16 "2022-02-23T16:43:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [23 February 2022 16:59 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/17 "2022-02-23T16:59:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 February 2022 17:00 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/18 "2022-02-23T17:00:05Z")

</div>

And the payload values?

---

<div class="post-metadata">

**Author:** ![Leopoll](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leopoll/32/70361_2.png) [@Leopoll](https://discourse.nodered.org/u/Leopoll)\
**Post date:** [23 February 2022 17:04 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/19 "2022-02-23T17:04:16Z")

</div>

payload values ​​from motion sensor i can do anything

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 February 2022 17:17 UTC](https://discourse.nodered.org/t/problem-with-current-state/58599/20 "2022-02-23T17:17:32Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/6/c6494688b2918c1c19f7f1efe079a320686c6645.png)

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

```

[Next page](https://discourse.nodered.org/t/problem-with-current-state/58599.md?page=2)
