I have an automation rule set up that should turn on the terrace lights if sun level is below 3.5 and me or my wife are at home. The third condition should prevent the automation to fire repeatedly and its value is true when entered into the template sections of developer tools.
Despite this the automation does not fire. Manually executing it turns on the lights normally. Any idea what is wrong in the script?
Thanks
alias: Terrace lights on
description: ''
trigger:
- platform: template
value_template: '{{ state_attr(''sun.sun'', ''elevation'') < -3.5 }}'
condition:
- condition: or
conditions:
- condition: state
entity_id: person.rentyo
state: home
- condition: state
entity_id: person.tibor
state: home
- condition: template
value_template: >-
'{{ as_timestamp(now()) -
(as_timestamp(state_attr("automation.terrace_lights_on",
"last_triggered")) or 0) > 21600 }}'
action:
- service: switch.turn_on
data: {}
entity_id: switch.terasa
- service: notify.notify
data:
message: 'Sun elevation is {{ state_attr("sun.sun", "elevation") }}'
title: Terrace lights are on
mode: single
so removing the delay condition seems to turn on the automation. however, another problem:
it seems when the conditions are not fullfilled when sun.elevation reaches -3.5 (no one is at home) the automation does not fire, but it waits 6 hours and if the conditions are fulfilled (somebody is at home) it turns the lights on. however I have a second rule set, which turns the lights off at 22:00 no matter what. so this behaviour turns on the lights after they should be turned off (and keeps them on for almost 24 hours).
What i want to achive is the following:
turn on the lights at sunset if somebody is home (this seems to work now)
if nobody was home at sunset, turn the lights on immediately when somebody arrives
turn the lights off at 22:00 and do not turn them on before next sunset, no matter who comes and goes
Do I need more automation rules for that, one triggered by sun and the second by person ?
I think the problem you’re having now is that the template trigger will fire every time there is a change in the sun elevation attribute, which is why it’s firing again after the delay.
Try using this trigger instead, along with the triggers for when someone arrives home:
alias: Terrace lights on
description: ''
trigger:
- platform: numeric_state
entity_id: sun.sun
attribute: elevation
below: -3.5
- platform: state
entity_id:
- person.rentyo
- person.tibor
to: 'home'
condition:
- condition: template
value_template: "{{ state_attr(''sun.sun'', ''elevation'') < -3.5 }}"
- condition: time
before: '22:00:00'
- condition: or
conditions:
- condition: state
entity_id: person.rentyo
state: 'home'
- condition: state
entity_id: person.tibor
state: 'home'
action:
- service: switch.turn_on
data: {}
entity_id: switch.terasa
- service: notify.notify
data:
message: 'Sun elevation is {{ state_attr("sun.sun", "elevation") }}'
title: Terrace lights are on
mode: single