Enable light when door open (between times and set duration)

I have a door upstairs and if it stays open for a certain amount of time (door sensor) I want a light to turn into a specific color. This only needs to happen in the evening.
Once the door closes, the original light setting should become active again (color/brightness)

As I am quite new to Node-RED I am struggling with this, hopefully someone can help.

Welcome to the forum @Piet200

If you have not already seen them then I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot in about 1 hour. A small investment for a lot of gain.

Have a look at the Trigger node. If you configure it to send nothing, then wait a certain time, then send the colour change message. Also configure it to reset the timer if a door close message is received (based on the payload value probably). That will change the colour if the door stays open for the specified time.
Then have a look at node-red-contrib-simple-gate. That can be used to block messages when required. You can use node-red-contrib-cron-plus to close and open the gate so that the colour change messages can only get through at certain times of day.
Finally, you can use a door close message to set the colour back to normal.

Thanks for the link to the usefull Node-RED instructions.
I managed to get a bit further but still looking for a few things.

  • action when opening the door works fine (including delay and only in the evening)
    But there is no action when the door closes again, cannot find how to do that.

  • didn't manage to get the current state of the light yet. so cannot revert to the original state.

Hopefully someone can point me in the right direction.

You can paste an image directly in here, no need to save it somewhere.

Do you have a message coming in with a payload that tells you what the current state is ("Open", "Close" or similar). If so the feed that through a Switch node and send the open and close messages to two outputs. Then on the Close output you can put whatever you need it to do.

You haven't told us anything about what the light is so difficult to help with that.

Hi,

Sorry for the delay, I was figuring things out.
I didn't know I could you the switch node, this is working fine now. thanks for the tip.

I am trying to fix the following: change color of light. see image. I don't know how to change the color, now it just is white. any tips?

Also as mentioned, I want to store the current light setting, which I can use afterwards to restore that setting. No idea how to do that.


See Working with context : Node-RED for how to use Context for saving data.

The colorings are working fine now.
I need a bit more assistance on the following:

Save current state of light.
The flow above does most things well (delay 1s is short, just for testing). but once the door closes it should go back to the default again. how does my flow look like getting this working?

The timer to change the light color will be set to 5 minutes. If the door closes within that period, the light should stay unchanged. I don't think this will work in the flow I have posted above.

no one a suggestion how to save and restore settings of the light ?

Yep as Colin said - use a change node to grab the incoming message that the light is on and store that in the flow context - you can then use the switch node to check that value in context and act appropriately

Further to that - put a debug node on the output of the two change nodes so we can see what messages are being sent and we can then tell you what to add to change nodes

As you already have change ndoes in there you should be able to add additional steps to them to set the flow context to say open or close at that point (rather than add additional change nodes)

Craig

Thanks, I am trying to understand, but I don't exactly know what to do.
Below the sceenshot of ty change node, switch node and debug output as you mentioned. What should I do with it?

switch-node
change-node

In the change node - use the dropdown and change it from msg to flow and give it a name (such as HallLightState) and set it to the Value of On

You can then use a Switch node to check the flow state variable and see if it is on or off and take appropriate action

Craig

Ok, so I need 2 switch nodes?
and in between the change node?
Like the schema I attached ?

Light.pdf (43.7 KB)

Wherever you change the state of the light you need to record it in your state context so it can be referred to

So yes after each of your current switches - which is where you are testing for something - you then include a change node to update the flow variable based on the result of the switch - so if you are testing if the door is open in your switch node then you put in a change node after that to set the flow state to record that result.

Craig

ok, but how does my flow look like. I am a bit confused.
The state of the light needs to be stored. when the door stays open it should be set to a different color. when the door closes again it should go back to the previous state.

Somehow I need to make the right connections, but which?
And the top Light node contains a fixed color code, it doesn't use the saved setting in the switch config.

How does the right switch now it needs to save the light setting? there is no configuration that say so. Although I have entered the flow setting as you suggested, but there is no real connection to the light itself.

Try this to get you on the path

[{"id":"91ee800c.97882","type":"inject","z":"56d3c1c6.0d901","name":"Door Open","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Open","payloadType":"str","x":210,"y":140,"wires":[["f2e11a79.f8f9c8"]]},{"id":"9523cd6b.1bd71","type":"inject","z":"56d3c1c6.0d901","name":"Door Closed","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Closed","payloadType":"str","x":220,"y":280,"wires":[["f2e11a79.f8f9c8"]]},{"id":"f2e11a79.f8f9c8","type":"switch","z":"56d3c1c6.0d901","name":"Monitor Door State","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Open","vt":"str"},{"t":"eq","v":"Closed","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":420,"y":220,"wires":[["c4cc5acf.ad66d8"],["8a95533a.011cd"]]},{"id":"c4cc5acf.ad66d8","type":"change","z":"56d3c1c6.0d901","name":"Set COntext for Door to Open","rules":[{"t":"set","p":"DoorState","pt":"flow","to":"Open","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":200,"wires":[["e4efbbfa.3a0358"]]},{"id":"8a95533a.011cd","type":"change","z":"56d3c1c6.0d901","name":"Set Context to Door Closed","rules":[{"t":"set","p":"DoorState","pt":"flow","to":"Closed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":260,"wires":[["ae9d0d4a.80ab8"]]},{"id":"e4efbbfa.3a0358","type":"trigger","z":"56d3c1c6.0d901","name":"Pause on Door Open","op1":"Open","op2":"StillOpen","op1type":"str","op2type":"str","duration":"1","extend":true,"overrideDelay":false,"units":"s","reset":"Open","bytopic":"all","topic":"topic","outputs":2,"x":1020,"y":160,"wires":[["251d4d76.9e62a2"],["80347b0e.3b4868"]]},{"id":"41fafddd.381444","type":"comment","z":"56d3c1c6.0d901","name":"Door Open","info":"Send to the Ligth Node to switch on","x":1220,"y":100,"wires":[]},{"id":"251d4d76.9e62a2","type":"debug","z":"56d3c1c6.0d901","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1230,"y":140,"wires":[]},{"id":"ff601f50.cfafa","type":"comment","z":"56d3c1c6.0d901","name":"Door Left Open","info":"Change the color of the light as door open too long","x":1240,"y":260,"wires":[]},{"id":"80347b0e.3b4868","type":"debug","z":"56d3c1c6.0d901","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1230,"y":300,"wires":[]},{"id":"4f75983f.a459c8","type":"comment","z":"56d3c1c6.0d901","name":"Door Closed - Switch off light","info":"Turn the light off","x":1060,"y":360,"wires":[]},{"id":"ae9d0d4a.80ab8","type":"debug","z":"56d3c1c6.0d901","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1010,"y":400,"wires":[]}]

As i do not have your light nodes i have put in debug nodes

Craig

Thank you very much.
I have imported the flow and have two questions about it.

I added the light node at the end of the flow, for example at the end, 'set context to door closed', but how does it use the previous state configuration? Now it just uses the color that is defined in the node. (or just white when no color is defined)

When the door left open, there is a different color defined. but what if the door closes after a while, then it should go back to the previous state. should I add another link for that ?

Well you have a couple of instances here

  1. Door opened - light turned on - presumably to last colour set ? or is it to a default colour ? If it is to a default colour then you do not need to do anything extra (presumably) as your Turn on Light node will set that then - if it is to a different colour then i would set another Flow Variable - that stored the value of the colour - not sure how you are setting this is is HVS, RGB or just by name ? But whatever it is then store that at the time you set it and put in the flow context

  2. Door left open to long - colour changes - presumably to a new fixed colour ? Nothing special to do here - you already have the trigger node that times out so you just set the new value in that object that switches the light

  3. If the door closes then you need to negate the trigger node so you will need a link into the trigger from the close switch - like this

[{"id":"91ee800c.97882","type":"inject","z":"56d3c1c6.0d901","name":"Door Open","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Open","payloadType":"str","x":210,"y":140,"wires":[["f2e11a79.f8f9c8"]]},{"id":"9523cd6b.1bd71","type":"inject","z":"56d3c1c6.0d901","name":"Door Closed","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Closed","payloadType":"str","x":220,"y":280,"wires":[["f2e11a79.f8f9c8"]]},{"id":"f2e11a79.f8f9c8","type":"switch","z":"56d3c1c6.0d901","name":"Monitor Door State","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Open","vt":"str"},{"t":"eq","v":"Closed","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":420,"y":220,"wires":[["c4cc5acf.ad66d8"],["8a95533a.011cd"]]},{"id":"c4cc5acf.ad66d8","type":"change","z":"56d3c1c6.0d901","name":"Set COntext for Door to Open","rules":[{"t":"set","p":"DoorState","pt":"flow","to":"Open","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":750,"y":200,"wires":[["e4efbbfa.3a0358"]]},{"id":"8a95533a.011cd","type":"change","z":"56d3c1c6.0d901","name":"Set Context to Door Closed","rules":[{"t":"set","p":"DoorState","pt":"flow","to":"Closed","tot":"str"},{"t":"set","p":"reset","pt":"msg","to":"Yes","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":260,"wires":[["ae9d0d4a.80ab8","e4efbbfa.3a0358"]]},{"id":"e4efbbfa.3a0358","type":"trigger","z":"56d3c1c6.0d901","name":"Pause on Door Open","op1":"Open","op2":"StillOpen","op1type":"str","op2type":"str","duration":"1","extend":true,"overrideDelay":false,"units":"s","reset":"Open","bytopic":"all","topic":"topic","outputs":2,"x":1020,"y":160,"wires":[["251d4d76.9e62a2"],["80347b0e.3b4868"]]},{"id":"41fafddd.381444","type":"comment","z":"56d3c1c6.0d901","name":"Door Open","info":"Send to the Ligth Node to switch on","x":1220,"y":100,"wires":[]},{"id":"251d4d76.9e62a2","type":"debug","z":"56d3c1c6.0d901","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1230,"y":140,"wires":[]},{"id":"ff601f50.cfafa","type":"comment","z":"56d3c1c6.0d901","name":"Door Left Open","info":"Change the color of the light as door open too long","x":1240,"y":260,"wires":[]},{"id":"80347b0e.3b4868","type":"debug","z":"56d3c1c6.0d901","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1230,"y":300,"wires":[]},{"id":"4f75983f.a459c8","type":"comment","z":"56d3c1c6.0d901","name":"Door Closed - Switch off light","info":"Turn the light off","x":1060,"y":360,"wires":[]},{"id":"ae9d0d4a.80ab8","type":"debug","z":"56d3c1c6.0d901","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1010,"y":400,"wires":[]}]

Craig

Hi, thank you very much for all you help and support. I am not sure if we have the same view on the achieved goal. I'll try to explain.

When the door opens, nothing should happen with the light. Unless the door stays open for longer than 5 minutes. then I'd like to set a specific color to the light. Onces the door closes again (for example after 6 minutes) the light should be set back to its previous state, wich can be either a different color, or just switched off, if it wasn't on before.

Is your flow configured like this?

Ok so say it like this

Trigger on door open

  1. Capture current state of light/colour
  2. Start 5 minute timer
  3. If door closes kill timer
  4. if timer expires (i.e. open longer than 5 minutes) then change the light colour
  5. Once door closed - then set the ligth back to previous colour

Is that right ?

Craig

Yes, that is exactly it.