React to an event only once

Hello, newbie here so, please bear with me.

Days ago I created a simple automation involving a light (lux) sensor and a couple of external smart lights:

  • Is the light sensor below 10 lux?
  • If so, turn on the exterior lights
  • Every day, at 10 pm, turn them off.

The automation works perfectly. Now, here comes the catch: I wanted to convert this automation to Node-Red, piece of cake right?, well, no. Here is my converted ‘version’:

And here is the code:

[{"id":"c7598497bd67830e","type":"server-state-changed","z":"22f1f34ef85f0be6","name":"Is light sensor below 10 lux?","server":"31a7d703.5ff578","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.tsl2591_light_sensor_lux","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"10","halt_if_type":"num","halt_if_compare":"lt","outputs":2,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"},{"property":"service","propertyType":"msg","value":"turn_on","valueType":"str"}],"x":140,"y":2680,"wires":[["6a26aa2efe910768"],[]]},{"id":"6a26aa2efe910768","type":"api-call-service","z":"22f1f34ef85f0be6","name":"External Lights ON/OFF","server":"31a7d703.5ff578","version":5,"debugenabled":false,"domain":"light","service":"{{service}}","areaId":[],"deviceId":[],"entityId":["light.entrada_luz_inferior","light.garaje_luz_superior_on_off"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":470,"y":2700,"wires":[[]]},{"id":"038fb745c2b90810","type":"inject","z":"22f1f34ef85f0be6","name":"Turn lights off (daily at 10:00 PM)","props":[{"p":"service","v":"turn_off","vt":"str"}],"repeat":"","crontab":"00 22 * * *","once":false,"onceDelay":0.1,"topic":"","x":180,"y":2740,"wires":[["6a26aa2efe910768"]]},{"id":"31a7d703.5ff578","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

The flow does work as expected but for one thing, lights are being turned on early in the morning when the lux sensor catches the first luminosity at sunrise, a reading below 10 that triggers the flow and turns the lights back to ‘on’.

How do you prevent this?

Any help will be appreciated.

Cheers from rainy Costa Rica.

I assume the problem is because the state is actually changing - for example going from 8 to 9. Even though you have ignore “Current state equals previous state”, it’s not really useful in this case.

There are a lot of ways you could fix this. One way would be to use “Trigger: State” instead. That can also test that the previous state was over 10 lux. Therefore, going from 8 to 9 would not trigger.

However, I would probably solve it by creating a “dark” binary sensor that was “on” if lux was under 10, and “off” otherwise. Then you could trigger based on this sensor instead. If the lux goes from 8 to 9, it would still be “on” and therefore not change state. Furthermore, you could use delay_on and delay_off to avoid the light going on/off/on if the lux was 9/10/9 etc.

1 Like

I would set up a schedule (light scheduler) for the main control of your lights, then use your lux sensor on its own schedule for the times that are outside of your primary light schedule.

1 Like

The event state node shows the old and new state in the data provided, so you can check if it has moved from 10 to 9. If you only want to turn the lights on in the evening (because in the morning light may go above 10 and then go down again), then use a time range node to check within the allowable range.

1 Like

Unlike YAML, Node Red is an event based tool. So, with that event: state sensor you set there, if light goes from, for example, 2 to 4, the node will render true and turn on the lights, which I believe it’s what’s happening to you in the mornings. You can limit that with a trigger: state node.

[{"id":"6a26aa2efe910768","type":"api-call-service","z":"878e74c2.7f39c8","name":"External Lights ON/OFF","server":"31a7d703.5ff578","version":5,"debugenabled":false,"service_domain":"light","service":"{{service}}","entityId":["light.entrada_luz_inferior","light.garaje_luz_superior_on_off"],"data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":690,"y":2660,"wires":[[]]},{"id":"038fb745c2b90810","type":"inject","z":"878e74c2.7f39c8","name":"Turn lights off (daily at 10:00 PM)","props":[{"p":"service","v":"turn_off","vt":"str"}],"repeat":"","crontab":"00 22 * * *","once":false,"onceDelay":0.1,"topic":"","x":400,"y":2700,"wires":[["6a26aa2efe910768"]]},{"id":"1ae8e88.8516918","type":"trigger-state","z":"878e74c2.7f39c8","name":"light sensor below 10 lux?","server":"9405c3fe.d0a6c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.tsl2591_light_sensor_lux","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":"<=","comparatorValueDatatype":"num","comparatorValue":"10","propertyValue":"new_state.state"},{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":">","comparatorValueDatatype":"num","comparatorValue":"10","propertyValue":"new_state.state"}],"inputs":0,"outputs":3,"customoutputs":[{"messageType":"custom","messageValue":"{\"service\": \"turn_on\"}","messageValueType":"json","comparatorPropertyType":"always","comparatorType":"is","comparatorValue":"","comparatorValueDataType":"str","comparatorPropertyValue":""}],"outputinitially":false,"state_type":"num","enableInput":false,"x":430,"y":2640,"wires":[[],[],["6a26aa2efe910768"]]},{"id":"31a7d703.5ff578","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]
1 Like

In this particular flow you want to build I would suggest, if I may, that you pushed it a bit further. To cope with the light sensor fluctuations, I’d do the following:

[{"id":"6a26aa2efe910768","type":"api-call-service","z":"878e74c2.7f39c8","name":"External Lights ON/OFF","server":"31a7d703.5ff578","version":5,"debugenabled":false,"service_domain":"light","service":"{{service}}","entityId":["light.entrada_luz_inferior","light.garaje_luz_superior_on_off"],"data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":790,"y":2760,"wires":[[]]},{"id":"038fb745c2b90810","type":"inject","z":"878e74c2.7f39c8","name":"Turn lights off (daily at 10:00 PM)","props":[{"p":"service","v":"turn_off","vt":"str"}],"repeat":"","crontab":"00 22 * * *","once":false,"onceDelay":0.1,"topic":"","x":400,"y":2900,"wires":[["6a26aa2efe910768"]]},{"id":"1ae8e88.8516918","type":"trigger-state","z":"878e74c2.7f39c8","name":"light sensor below 10 lux?","server":"9405c3fe.d0a6c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.tsl2591_light_sensor_lux","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":"<=","comparatorValueDatatype":"num","comparatorValue":"10","propertyValue":"new_state.state"},{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":">","comparatorValueDatatype":"num","comparatorValue":"10","propertyValue":"new_state.state"}],"inputs":0,"outputs":3,"customoutputs":[{"messageType":"custom","messageValue":"{\"service\": \"turn_on\"}","messageValueType":"json","comparatorPropertyType":"always","comparatorType":"is","comparatorValue":"","comparatorValueDataType":"str","comparatorPropertyValue":""}],"outputinitially":false,"state_type":"num","enableInput":false,"x":290,"y":2700,"wires":[[],[],["96f39607.1277c8"]]},{"id":"e85456f3.e2d598","type":"trigger-state","z":"878e74c2.7f39c8","name":"light sensor above 10 lux?","server":"9405c3fe.d0a6c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"sensor.tsl2591_light_sensor_lux","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":">=","comparatorValueDatatype":"num","comparatorValue":"10","propertyValue":"new_state.state"},{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":"<","comparatorValueDatatype":"num","comparatorValue":"10","propertyValue":"new_state.state"}],"inputs":0,"outputs":3,"customoutputs":[{"messageType":"custom","messageValue":"{\"service\": \"turn_off\"}","messageValueType":"json","comparatorPropertyType":"always","comparatorType":"is","comparatorValue":"","comparatorValueDataType":"str","comparatorPropertyValue":""}],"outputinitially":false,"state_type":"num","enableInput":false,"x":290,"y":2780,"wires":[[],[],["96f39607.1277c8"]]},{"id":"96f39607.1277c8","type":"trigger","z":"878e74c2.7f39c8","name":"","op1":"","op2":"","op1type":"pay","op2type":"payl","duration":"5","extend":false,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":530,"y":2760,"wires":[["6a26aa2efe910768"]]},{"id":"31a7d703.5ff578","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

This way if the there are rapid fluctuations in the light sensor from above 10 to below 10 you won’t find your light flashing on and off. Only 1 call every 5 minutes at most, latest call applies.

1 Like

That is exactly what happens. The solution you propose is interesting and I really appreciate the time you took to answer my question.

I’m going to try it. Thank you very much!

Nice trick, will use it as well. Thank you!

Can you explain this trigger? I thought with two conditions there, it will never be true since one is <= 10 and one is > 10. Am I not reading that right?

image

Update your home assistant web socket nodes in the palette menu in the right hamburger menu.

You don’t undestand it because there’s a mistake in both trigger state nodes. You are right, it should look like this for the below 10 node:

same goes for the other one but the opposite way.

1 Like