I feel this should be possible, but I can’t figure it out:
Instead of a trigger firing, I would like to create an automation that does something as long as a state is true.
Case in point: motion triggered lights: While motion sensor detects motion, keep light on, once sensor no longer detects motion (plus maybe a minute after that as long as no new motion is detected), switch light off again.
RIght now I am writing two automations for each light where I want this behaviour. One for on, one for off. But that adds up very quickly and makes it harder to maintain. I have tried using only the “motion detected trigger” and then set light on for x amount of time, but even after tweaking the timeout value, it still does not really fit in well in certain areas of the house.
My first advice would be to stick with separate automations. Own experience made: if you try to make things complicated (value templates, etc), they will become complicated. I prefer writing dedicated sets of tiny automations rather than pumping up the code.
Maybe you could create groups of light according to specific events? I have several Philips Hue lights in my living room which I grouped into several different groups so that I can launch actions according to the event or objective.
Next idea would be to simply structure your configuration file in a way that one section handles everything regarding “light turns on”, and another section is for “light turns off”. This is how you might keep better control. What I did in addition is to always mention the specific room (in the sub-sections), so I quickly find any associated automation rule if needed.
It’s certainly doable…with a caveat. If your sensors and lights are named similar, you can easily keep this to 2 automations. If they aren’t…it’s a little more difficult (still doable though). Here’s an example. Let’s say we have 3 sensors named sensor.<room> and 3 lights similarly named light.<room> , we can do this:
- alias: 'Turn Lights On With Motion'
initial_state: on
trigger:
- platform: state
entity_id: sensor.living_room
to: 'on'
- platform: state
entity_id: sensor.kitchen
to: 'on'
- platform: state
entity_id: sensor.bathroom
to: 'on'
action:
- service: light.turn_on
data_template:
entity_id: >-
{% set room = trigger.entity_id.split(".")[1] %}
{% set entity = ["light",room]|join(".") %}
{{entity}}
- alias: 'Turn Lights Off With No Motion'
initial_state: on
trigger:
- platform: state
entity_id: sensor.living_room
to: 'off'
for:
minutes: 2
- platform: state
entity_id: sensor.kitchen
to: 'on'
for:
minutes: 2
- platform: state
entity_id: sensor.bathroom
to: 'on'
for:
minutes: 2
action:
- service: light.turn_off
data_template:
entity_id: >-
{% set room = trigger.entity_id.split(".")[1] %}
{% set entity = ["light",room]|join(".") %}
{{entity}}
In the entity_id, we are using a template that retrieves the trigger. Let’s say it was the “sensor.kitchen” splits between the “.” and gives us the room of “kitchen” then pastes “light.” to the front of it. Pretty simple and works.
Now if you don’t have similarly named sensors and lights, it becomes more difficult, but still it’s possible. For instance, let’s say your sensors are all named something like this… “sensor.kitchen_motion” then we’d need to get rid of the “_motion” part of it too. And that can be done with something like this…
Of course, if your sensors and lights don’t follow ANY similar naming conventions, then again it’s possible with If, elif, else statements, but at that point, it’s probably best to just rename everything to something more standard. Hope that helps!