Configuration template for date range xmas lights

Template dumb here folks. So I reviewed this thread and wow amazing stuff but whew over my head: Automation During Date Range - #16 by m13kern

I modified this piece I found in a solution 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 )) }}

To be this:

  - condition: template # Only between Nov 26 and Jan 5.
    value_template: >
      {% set n = now() %}
      {{ ( n.month == 11 and ( 1 >= n.day >= 26 )) or ( n.month == 1 and ( 1 <= n.day <= 5 )) }}

Basically, I converted the January version to flip it to be starting the lights on Nov 26 each year and replaced the December 1 component with ( n.month == 11 and ( 1 >= n.day >= 26 )) .

Does this make sense / will it work? Or, did I fail my attempt? Help appreciated.

Why are you testing for 1 Nov, should it not be only beyond 26th?
Then, with below it will also trigger on Jan 5…the basis you used mentioned between and indeed excludes 6th…yours does not
e.g.

- condition: template # Only between Nov 26 and Jan 5.
    value_template: >
      {% set n = now() %}
      {{ ( n.month == 11 and n.day >= 26 ) or ( n.month == 1 and ( 1 <= n.day <= 5 )) }}

Ah gotcha. My ignorance so I assumed I needed that 1 >= n.day part since the other had it. Thank you for the help and explanation.

If you define the month and day as a tuple, the resulting template is not only more compact it’s also more legible.

  - condition: template # Only between Nov 26 and January 5.
    value_template: >
      {% set n = (now().month, now().day) %}
      {{ n >= (11, 26) or n <= (1, 5) }}

Would you mind explaining how this works?
EDIT: it checks the combination of two figures and both conditions need to match?

Copy-paste this into the Template Editor and the result will be false.

{{ (10, 27) < (1, 5) }}

So this ‘tuple’ is a date-only thing? Just asking :slight_smile:
EDIT, then it could also be … ?

{{ (11, 26) <= n <= (1, 5) }}

EDIT2…no, of course not… :slight_smile:

A tuple is like a list whose items cannot be modified after having been defined. It can contain numbers, strings, booleans, etc. When comparing tuples, each item in the first tuple is compared with its counterpart in the second tuple (first item to first item, second item to second item, etc).

For more information, search for “comparing tuples in python”.

1 Like

@123 Not sure what’s up but the lights came on last night with the new string. Here’s my yaml:

- id: '5000000100011'
  alias: Outside Christmas Lights On
  trigger:
  - platform: sun
    event: sunset
    offset: -00:12:00
#  condition: []
  condition:
  - condition: template # Only between Nov 26 and January 5.
    value_template: >
      {% set n = (now().month, now().day) %}
      {{ n >= (11, 26) or n <= (1, 5) }}
  action:
  - service: switch.turn_on
    target:
      entity_id: switch.outdoorplug

Did I do something wrong here?

If you copy-paste the template into the Template Editor it will report False because today, November 12, is not within the desired date range. A Template Condition that evaluates to False will prevent the automation from executing its action.

If the automation did execute at the offset sunset time yesterday, there will be a trace available showing exactly what happened. Check the automation’s trace.

Doh! I should’ve thought of that. Apologies, thank you.

For the fools like me who come here later, it was this that did it:

  alias: Xmas Outside Christmas Lights Off randomized
  trigger:
  - platform: sun
    event: sunset
    offset: -00:30:00
  - platform: time
    at: sensor.xmas_front_door
  action:
  - service: switch.turn_{{ 'on' if trigger.platform == 'sun' else 'off' }}
    target:
      entity_id: switch.outdoorplug

I’m not sure how best to merge these tasks except to add the same condition so it runs the same -or- dropping the variable to just be _off at the trigger time of the time-based sensor I have in configuration.yml.

template:
  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: homeassistant
        event: start
      - platform: event
        event_type: event_template_reloaded
    sensor:
      - name: livingroom_tree_off
        unique_id: livingroom_tree_off
        state: "{{ (today_at('22:10') + timedelta(minutes = range(2,36) | random)).isoformat() }}"
        device_class: timestamp
      - name: familyroom_tree_off
        unique_id: familyroom_tree_off
        state: "{{ (today_at('21:00') + timedelta(minutes = range(2,36) | random)).isoformat() }}"
        device_class: timestamp
      - name: xmas_front_door
        unique_id: xmas_front_door
        state: "{{ (today_at('22:20') + timedelta(minutes = range(2,36) | random)).isoformat() }}"
        device_class: timestamp
      - name: upstairs_wrap_lights
        unique_id: upstairs_wrap_lights
        state: "{{ (today_at('20:00') + timedelta(minutes = range(2,36) | random)).isoformat() }}"
        device_class: timestamp

I think I’ll just do this and hope for the best:

- id: '5000000100011'
  alias: Xmas Outside Christmas Lights On and then Off randomized
  trigger:
  - platform: sun
    event: sunset
    offset: -00:12:00
  - platform: time
    at: sensor.xmas_front_door
  condition:
  - condition: template # Only between Nov 26 and January 5.
    value_template: >
      {% set n = (now().month, now().day) %}
      {{ n >= (11, 26) or n <= (1, 5) }}
  action:
  - service: switch.turn_{{ 'on' if trigger.platform == 'sun' else 'off' }}
    target:
      entity_id: switch.outdoorplug

Have a great weekend!

So another automation was responsible for turning on the switch.

The consolidated example you posted should work.

If the suggested template meets your requirements, please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

Solution marked. Apologies thought I had already.

So, the date hit and it only stayed on for 29 seconds at 4:39pm which was sunset. I’m not sure what’s up after sifting through the logs.

This is the randomized sensor for turning off the script:

template:
  - trigger:
      - platform: time
        at: '00:00:00'
      - platform: homeassistant
        event: start
      - platform: event
        event_type: event_template_reloaded
    sensor:
      - name: xmas_front_door
        unique_id: xmas_front_door
        state: "{{ (today_at('22:20') + timedelta(minutes = range(2,36) | random)).isoformat() }}"
        device_class: timestamp

This is the automation:

- id: '5000000100011'
  alias: Xmas Outside Christmas Lights On and then Off randomized
  trigger:
  - platform: sun
    event: sunset
    offset: -00:12:00
  - platform: time
    at: sensor.xmas_front_door
  condition:
  - condition: template # Only between Nov 26 and January 5.
    value_template: >
      {% set n = (now().month, now().day) %}
      {{ n >= (11, 26) or n <= (1, 5) }}
  action:
  - service: switch.turn_{{ 'on' if trigger.platform == 'sun' else 'off' }}
    target:
      entity_id: switch.outdoorplug

Last year I had two separate automations for on/off, but I wanted to simplify it by combining thinking it would work. Where am I wrong? Any solutions?

Thank you!!!

Check the automation’s trace.

Can’t lie I am lost as shit. This is the only automation that uses the outdoor plug and the plug isn’t in any groups. It gets called to off twice. Screenshot below.

Sunset trace:

this:
  entity_id: automation.outside_christmas_lights_on
  state: 'on'
  attributes:
    last_triggered: null
    mode: single
    current: 0
    id: '5000000100011'
    friendly_name: Xmas Outside Christmas Lights On and then Off randomized
  last_changed: '2022-11-21T13:54:29.064218+00:00'
  last_updated: '2022-11-21T13:54:29.064218+00:00'
  context:
    id: 01GJD7G2J89GW1W6Y33F7AT0BC
    parent_id: null
    user_id: null
trigger:
  id: '0'
  idx: '0'
  alias: null
  platform: sun
  event: sunset
  offset:
    __type: <class 'datetime.timedelta'>
    total_seconds: -720
  description: sunset with offset

Night random off trace:

this:
  entity_id: automation.outside_christmas_lights_on
  state: 'on'
  attributes:
    last_triggered: null
    mode: single
    current: 0
    id: '5000000100011'
    friendly_name: Xmas Outside Christmas Lights On and then Off randomized
  last_changed: '2022-11-21T13:54:29.064218+00:00'
  last_updated: '2022-11-21T13:54:29.064218+00:00'
  context:
    id: 01GJD7G2J89GW1W6Y33F7AT0BC
    parent_id: null
    user_id: null
trigger:
  id: '1'
  idx: '1'
  alias: null
  platform: time
  now: '2022-11-25T22:40:00-05:00'
  description: time set in sensor.xmas_front_door
  entity_id: sensor.xmas_front_door

The Logbook reports that it was turned on at 3:39 pm and turned off at 6:55 pm (and turned off again at 10:03 pm). Where’s the “stayed on for 29 seconds at 4:39pm” you mentioned?

Or is the Logbook screenshot for a different day?

NOTE
Not sure if it’s relevant but I noticed the Logbook didn’t indicate who turned off the device. EDIT Nevermind, I checked my Logbook and it also doesn’t indicate that an automation turned off the light.

I may have misread it. No one is home and I’m the only one with access so the double turn off has me super confused. It seems also times display different for me since I’m in another time zone currently. Didn’t expect that.

The sensor script used 22:10 with a randomization on it so turning off when it did doesn’t make sense.

I’m confused as heck at why it turned off well before a randomization of 34 minutes ~1010pm.

Add a notification at the end that reports what it did and when it did it. Sometimes this is easier to understand what’s going on than interpreting a trace.

- id: '5000000100011'
  alias: Xmas Outside Christmas Lights On and then Off randomized
  trigger:
  - platform: sun
    event: sunset
    offset: -00:12:00
  - platform: time
    at: sensor.xmas_front_door
  condition:
  - condition: template # Only between Nov 26 and January 5.
    value_template: >
      {% set n = (now().month, now().day) %}
      {{ n >= (11, 26) or n <= (1, 5) }}
  action:
  - service: switch.turn_{{ 'on' if trigger.platform == 'sun' else 'off' }}
    target:
      entity_id: switch.outdoorplug
  - service: notify.persistent_notification
    data:
      title: "{{ now().timestamp() | timestamp_custom() }}"
      message: "Triggered by {{ trigger.platform }} and setting switch to {{ 'on' if trigger.platform == 'sun' else 'off' }}."

Thanks. Everything has worked fine since adding the dang notify statement for some reason. We haven’t been home so it was quite strange that there would have been any activity to turn off the outdoor plug. No one was on camera. Weirdness.