- 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.
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
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.