Help with date-dependent automation

I am trying to get a simple (I think!) automation (which turns on a light) to run every day, except on October 31st where I want to delay it by 5 hours. I thought I had set it up properly, but it did not work this past October 31st, and the light turned on when I did not want it to. Can someone point me to the error/problem in the code below and suggest a correction? Thanks!

id: 'XXX'
alias: Front porch lights on before sunset
description: ''
triggers:
  - trigger: sun
    event: sunset
    offset: '-00:20:00'
conditions: []
actions:
  - if:
      - condition: template
        value_template: '"{{ (now().month, now().day) == (10, 31) }}"'
    then:
      - delay:
          hours: 5
          minutes: 0
          seconds: 0
          milliseconds: 0
      - type: turn_on
        device_id: YYY
        entity_id: ZZZ
        domain: switch
    else:
      - type: turn_on
        device_id: YYY
        entity_id: ZZZ
        domain: switch
mode: single

What did the trace say?

Edit:

I see something…

value_template: '"{{ (now().month, now().day) == (10, 31) }}"'

You have double quotes.
I mean both single and double, so triple I guess…

Like this:

"{{ (now().month, now().day) == (10, 31) }}"

Or this:

'{{ (now().month, now().day) == (10, 31) }}'

Not like this:

'"{{ (now().month, now().day) == (10, 31) }}"'

For future reference, it’s inadvisable to use long delay statements (5 hours qualifies as long). If Home Assistant is restarted during the delay’s countdown, the automation is terminated immediately and the delay is aborted. On restart, the terminated automation does not resume from the point it was terminated.

Thanks both. I see the extra quotes, but I cannot figure out how to remove them. I set up the automation in the GUI using “Conditionally execute an action and default to another action” and then “If template renders a value equal to true”, and it added the double quotes and single quotes automatically. When I edit to remove the quotes and resave, they reappear. Any suggestions?

I just used the Automation Editor, in Visual Mode, to create a Template Condition and it did not add double and single quotes automatically. It wrapped my template in double quotes only.

Visual mode

YAML Mode


NOTE

Don’t do this (explicitly wrap it in single-quotes):

Because then you will get this (wrapped in two sets of quotes):

Thanks. When I look at the automation configuration (under automation - traces - automation configuration (which is where I copied from in my first post)), it shows the combination single and double quotes. But when I choose “edit in yaml”, only the double quotes show up. So now I am not sure what is going on.

In the UI you should not add any quotes to the templates.

Also, don’t use a 5 hour delay… just set a second trigger:

id: 'XXX'
alias: Front porch lights on before sunset
description: ''
triggers:
  - trigger: sun
    event: sunset
    offset: '-00:20:00'
  - trigger: sun
    event: sunset
    offset: '04:40:00'
    id: halloween
conditions:
  - condition: template
    value_template: "{{ (trigger.id == 'halloween') ==  ((now().month, now().day) == (10, 31)) }}"
actions:
  - type: turn_on
    device_id: YYY
    entity_id: ZZZ
    domain: switch
mode: single
1 Like

While editing the automation in the Automation Editor, switch its mode from visual to YAML. That’s what I displayed in the screenshots visible in my previous post. That’s where my testing shows no automatic inclusion of two kinds of quotes.

Anyways, I suggest you replace your existing automation with the one Didgeridrew suggested because it is more robust (does not use a 5 hour delay statement).