Condition for X-mas lights between Nov and Feb not working

I am pretty new to Home assistant and am trying to figure out why this automation isn’t working. The intent is to trigger several lights to turn on 2 hours before sunset with a condition to only work between November 1st and Feb 1st. This is what I have for the automation:

alias: Turn on x-mas lights
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: "-02:00:00"
    enabled: true
condition:
  - condition: template
    value_template: "{{ (11,1) <= (now().month, now().day) <= (2,1) }}"
    enabled: true
action:
  - type: turn_on
    device_id: 0f0991bc696b4a12e42b5029977e468b
    entity_id: switch.oustide_3_plug_lights_socket
    domain: switch
    enabled: true
  - type: turn_on
    device_id: 9d54754aba2d4357a94caab4e76e2447
    entity_id: switch.garage_x_mas_lights_socket
    domain: switch
    enabled: true
  - type: turn_on
    device_id: adf5bafbbe80417a9bc98d6193881190
    entity_id: 1f0ccaf91325fb7a0b7e2838d85fa0f3
    domain: switch
mode: single

I have other automations that trigger on lights 1 hour before sunset and just copied that, modified it to 2 hrs and tried to add the condition with the dates based on what Google Bard suggested, yet the automation doesn’t work. When I test the date template within the visual editor, it keeps giving me an error saying “conditions did not pass” even though the current date is 11/24/23. I did some digging into some other posts I could find on the topic (from 2019) and tried emulating what they had but to no avail.

What the hell am I missing? I feel like since HA went through the major updates that the templates no longer work or that maybe I’m just missing something really simple and obvious? Ugh!

In general, I am mostly used to working in the visual editor and can figure out the basics, but I am not at all confident with the Yaml stuff. I have also noticed that the stuff that I am getting from Bard don’t quite match up with the current layout and build of HA.

Any help is appreciated!
Cheers!
-Matt

A date cannot be both after Nov AND before Feb (because there is no year in the test) - you need to use OR.

Ok, but when I try this condition i found in another post:

- condition: template # Only between December 1 and January 6.
    value_template: >
      {% set n = now() %}
      {{ n.month == 12 or ( n.month == 1 and ( 1 <= n.day <= 5 )) }}

and change it to 11 instead of 12, I still get the same error of “Condition did not pass”. Does this one also require a year?

The template is fine, your indentation is not.

- condition: template # Only between December 1 and January 6.
  value_template: >
    {% set n = now() %}
    {{ n.month == 12 or ( n.month == 1 and ( 1 <= n.day <= 5 )) }}

OK- I got this condition to pass:

condition:
  - condition: template
    value_template: |
      {% set n = now() %} {{ n.month == 11 or n.month == 12 or n.month == 1 }}

Hopefully this will trigger the lights to come on tomorrow! Thanks for the help so far!

Update: Yup- it worked! Yay!

condition:
  - condition: template
    value_template: "{{ now().month in [11, 12, 1] }}"

More examples here:

2 Likes