- 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 )) }}
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”.
@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
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.
- 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
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.
- 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?
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.
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.