So some of my lights, maddeningly, sometimes don’t switch on or off when they are supposed to so I wrote an automation to turn them on or off a second and third time straight away and again after a delay of 5 seconds. It looks like this:
- id: '1532478833426'
alias: Lights Off Failsafe
initial_state: 'on'
trigger:
- platform: state
from: 'on'
to: 'off'
entity_id: light.lounge, light.dining, light.spots, light.office
condition: []
action:
- service: light.turn_off
data_template:
entity_id: >-
{%- if (as_timestamp(now()) - as_timestamp(states.light.lounge.last_changed) < 5) and (as_timestamp(now()) - as_timestamp(states.light.spots.last_changed) < 5) -%}
light.lounge, light.spots
{%- elif (as_timestamp(now()) - as_timestamp(states.light.office.last_changed) < 5) and (as_timestamp(now()) - as_timestamp(states.light.spots.last_changed) < 5) -%}
light.office, light.spots
{%- elif as_timestamp(now()) - as_timestamp(states.light.spots.last_changed) < 5 -%}
light.spots
{%- elif as_timestamp(now()) - as_timestamp(states.light.office.last_changed) < 5 -%}
light.office
{%- elif as_timestamp(now()) - as_timestamp(states.light.lounge.last_changed) < 5 -%}
light.lounge
{%- else -%}
light.dining
{%- endif -%}
- delay: '00:00:05'
- service: light.turn_off
data_template:
entity_id: >-
{%- if (as_timestamp(now()) - as_timestamp(states.light.lounge.last_changed) < 15) and (as_timestamp(now()) - as_timestamp(states.light.spots.last_changed) < 15) -%}
light.lounge, light.spots
{%- elif (as_timestamp(now()) - as_timestamp(states.light.office.last_changed) < 15) and (as_timestamp(now()) - as_timestamp(states.light.spots.last_changed) < 15) -%}
light.office, light.spots
{%- elif as_timestamp(now()) - as_timestamp(states.light.spots.last_changed) < 15 -%}
light.spots
{%- elif as_timestamp(now()) - as_timestamp(states.light.office.last_changed) < 15 -%}
light.office
{%- elif as_timestamp(now()) - as_timestamp(states.light.lounge.last_changed) < 15 -%}
light.lounge
{%- else -%}
light.dining
{%- endif -%}
These are limitlessled lights and are grouped of course and I find 1 of the 4 sometimes doesn’t turn on or off the first time.
I’m pretty sure I have the template right as it seems to be triggered correctly in the editor but I’m still finding it’s not turning on or off correctly…
Any ideas? Maybe I have the times wrong or something stupid like that.