Automation won't fire with template condition

I am pulling my hair out and I am hoping someone can see what I am doing wrong. I have an automation that raises and lowers my shades once daily.

Morning condition:

  • Must be after 7:15PM
  • Must be after sunrise.

The intent is to prevent the shades from going up at 6AM in the summer - but going up at 7:45AM in the winter is ok.

Night condition:

  • Must be triggered by a “night” trigger ID - be it sunset OR 8PM
  • Must not have run in the past 4 hours.

The idea here is the shades can go down at sunset so far as sunset is before 8PM. However in the summer when sunset is at 9PM, put the shades down no later than 8PM. Then, the template says to not run again if there was a run at 8PM.

Here is my automation:

alias: "Shades - Front Parlor "
description: Front Parlor Shade Control
trigger:
  - platform: sun
    event: sunset
    offset: 0
    id: night
  - platform: time
    at: "20:00:00"
    id: night
  - platform: sun
    event: sunrise
    offset: 0
    id: day
  - platform: time
    at: "07:15:00"
    id: day
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: sun.sun
            state: above_horizon
          - condition: trigger
            id: day
        sequence:
          - service: notify.activity_log
            data:
              message: "{{ now().strftime('%m/%d/%Y %H:%M:%S') }} Shades up."
          - service: rest_command.front_parlor_shade_toggle
            data: {}
      - conditions:
          - condition: trigger
            id: night
          - condition: template
            value_template: >-
              {{ (as_timestamp(utcnow()) | round ) -
              (as_timestamp(state_attr('automation.shades_front_parlor','last_triggered'))
              | round ) > 14400 }}
        sequence:
          - service: notify.activity_log
            data:
              message: "{{ now().strftime('%m/%d/%Y %H:%M:%S') }} Shades down."
          - service: rest_command.front_parlor_shade_toggle
            data: {}
mode: single

THE PROBLEM: The night never runs. At this time of year it doesn’t run at 8PM nor at sunset at 8:45PM. I have confirmed that the last run of the automation was indeed at 715AM andf there have been no subsequent executions. However, the night trace shows it goes to the condition and does nothing. Additionally, as I look at the template in the Dev Tools, it shows exactly what I would expect. This output is my current time as the morning execution is about 3 hours 30 mins ago, but the math and template evaluation is correct.

{{ (as_timestamp(utcnow()) | round ) - (as_timestamp(state_attr('automation.shades_front_parlor','last_triggered')) | round ) > 14400 }}

{{ (as_timestamp(utcnow()) | round ) - (as_timestamp(state_attr('automation.shades_front_parlor','last_triggered')) | round ) }}

{{ as_timestamp(state_attr('automation.shades_front_parlor','last_triggered')) | round }}

{{ (as_timestamp(utcnow()) | round ) }}
False

12660

1684926900

1684939560

What am I missing?

Your condition is failing because the automation triggers (which updates last_triggered) then you’re checking last_triggered… use the self-referencing variable this instead:

alias: "Shades - Front Parlor"
description: Front Parlor Shade Control
trigger:
  - platform: sun
    event: sunset
    offset: 0
    id: night
  - platform: time
    at: "20:00:00"
    id: night
  - platform: sun
    event: sunrise
    offset: 0
    id: day
  - platform: time
    at: "07:15:00"
    id: day
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: day
          - condition: state
            entity_id: sun.sun
            state: above_horizon
        sequence:
          - service: notify.activity_log
            data:
              message: "{{ now().strftime('%m/%d/%Y %H:%M:%S') }} Shades up."
          - service: rest_command.front_parlor_shade_toggle
            data: {}
      - conditions:
          - condition: trigger
            id: night
          - condition: template
            value_template: |-
              {{ now() - this.attributes.last_triggered > timedelta(hours=4) }}
        sequence:
          - service: notify.activity_log
            data:
              message: "{{ now().strftime('%m/%d/%Y %H:%M:%S') }} Shades down."
          - service: rest_command.front_parlor_shade_toggle
            data: {}
mode: single
1 Like

I had no idea that existed. And duh, that makes perfect sense in retrospect. It’s never > 14400 as it triggered a millisecond ago. Thank you!