I have an automation set up to turn a porch light on every night at sunset, and it works great. I’m trying to figure out how to do a different color based on if the date is a holiday. Of course I could do it with an individual automation for each holiday, but is there a way to do it in a single automation? Thanks.
Certainly. You can even use the automation you already have.
All it needs to do is check if the current date matches any one of the holidays that interest you. It can use a dictionary containing dates (month and day) and the associated color to set the light.
Let me know if this approach interests you and I’ll help you create it.
Thank you, that would be awesome. Here is my current automation:
alias: Turn on patio light at sunset.
description: ' '
trigger:
- platform: sun
event: sunset
condition: []
action:
- service: light.turn_on
data:
brightness_pct: 100
color_name: green
entity_id: light.outside 2
mode: single
That entity_id
can’t be right. Spaces are not allowed in an entity’s object_id (the name to the left of the word “light.”) so I assume there’s an underscore between “outside” and “2”?
alias: Turn on patio light at sunset.
description: ' '
trigger:
- platform: sun
event: sunset
condition: []
action:
- service: light.turn_on
data:
brightness_pct: 100
color_name: >
{% set holidays = { '03-17': 'green', '07-01': 'blue',
'10-31': 'orange', '12-25': 'red',
'01-01': 'yellow' } %}
{% set today = (now().date()|string)[5:] %}
{{ holidays[today] if today in holidays.keys() else 'white' }}
entity_id: light.outside_2
mode: single
Change the word white
to whatever color you normally want for non-holidays.
To test the automation, you don’t have to wait for a holiday. Just add tomorrow’s date to the dictionary.
{% set holidays = { '03-17': 'green', '07-01': 'blue',
'10-31': 'orange', '12-25': 'red',
'01-01': 'yellow', '12-16': 'purple' } %}
This is a HUGE help Thank you!
Taras the way I read that script above, it only sets that light color for a specific day and then reverted back to white. Lets say you wanted to just change a light color for an entire month period and then revert back to a default color. What would that script look like then?
I would use what I suggested here:
@123 I have tried to replicate the yaml code that you provided to alter an existing automation. Here is my automation:
alias: “Automation_night: Holiday lights on”
description: “”
trigger:
- platform: sun
event: sunset
offset: “+00:30:00”
condition: []
action: - service: light.turn_on
data: {}
target:
entity_id:
- light.outletlinc_dimmer_fence
- light.outletlinc_dimmer_outdoorplug
device_id:
- ddadfef10e6968c8f43aabd5d77c1b4d
enabled: true - service: switch.turn_on
data: {}
target:
entity_id:
- switch.pergola
- switch.front_garden
- switch.back_garden
- switch.switchlinc_relay_frontdoor
mode: single - service: light.turn_on
data:
brightness_pct: 100
color_name: >
{% set holidays = { ‘03-17’: ‘green’, ‘07-01’: ‘red’,
‘10-31’: ‘orange’, ‘12-25’: ‘red’,
‘02-14’: ‘red’ } %}
{% set today = (now().date()|string)[5:] %}
{{ holidays[today] if today in holidays.keys() else ‘white’ }}
target:
entity_id:
- light.sofa
- light.orphan
mode: single
For some reason, the automation does not save. When I go back into my automation, the yaml code appears blank:
alias: holiday test
description: “”
trigger: []
condition: []
action: []
mode: single
Any thoughts?
Format the automation you posted because, in its unformatted state, I can’t check it for indentation errors.
This is super helpful, thanks. I’m brand new to HA and trying to set up something similiar, but not sure how I could do a date range instead of just a single day? For example, setting the lights to red for the span of 12-19 to 12-31.
I also have some preset scenes I’d like to call instead of just plain colors - scene.outdoor_lights_rox_wiz for example. Is that possible as well? TIA
Unfortunately, I don’t have free time to redesign that 3 year-old example for your needs. Perhaps someone else can do that for you. If not, just add each individual day, in your desired date-range, to the holidays
dictionary within the template.
Thanks. Wasn’t expecting you to write code for me, just curious if there was an easy way to add ranges. I’ll do some searching. Appreciate the reply.
Here’s how I would do nowadays (just be sure the dictionary keys don’t have overlapping date-ranges).
alias: Turn on patio light at sunset.
description: ' '
trigger:
- platform: sun
event: sunset
condition: []
action:
- service: light.turn_on
target:
entity_id: light.outside
data:
brightness_pct: 100
color_name: >
{% set today = (now().month, now().day) %}
{% set holidays = {
(1,1): 'yellow',
(3,17): 'green',
(7,1): 'blue',
(9,12) <= today <= (9,29): 'purple',
(10,31): 'orange',
(12,19) <= today <= (12,29): 'red' } %}
{% if today in holidays.keys() %}
{{ holidays[today] }}
{% elif true in holidays.keys() %}
{{ holidays.get(true) }}
{% else %}
white
{% endif %}
mode: single
I added an extra date range in the example, for September 12 through 29th, just so you can see it in action this month.
Thanks for this, super helpful!
Now I just have to teach myself better yaml so I can get it working with variable entities.
So much easier( and way miore powerful) to use Google Calendar integration and… no need to use Yaml.
Can you share an example?
Here is what I did. I use the Calander events. I then use a scene to design what I want the settings to be. Then I use an automation that gets triggered by the event in the schedule. It all works nicely. I have a normal scene that returns the lights back to normal as scenes are fire and forget and not stateful as I see them. Note colors for scenes are broken for some entities and you might have to modify the yaml of the scene which is a HA issue they are working on. This year I will work to make it a blueprint for everyone. Here is the code for the automation. I have one scene that is the standard scene. For the scenes I have they have state. Meaning they turn on and off when they are lights or switches for their intended duration in the schedule. So I first turn off the standard light scene then I turn on the holiday scenes. When done I turn on the Standard scene again. My standard scene stays on until morning for security reasons.
So first add the local Calander to the config
Local calendar - Home Assistant (home-assistant.io)
Then add your Standard Outdoor Scene
Then add your holidays scenes (keep name simple like Christmas)
Then add your holidays in the Calander (Same name as the scene name for it to work)
Then load this automation. You will need to update the cal and standard scene. Do not forget to look in code for the standard scene name as I have it embedded as well and have not done code clean up yet. Again I have problems with scenes keeping their color effect which is a different problem and will over time just resolve itself at some point. Work on getting the scenes working correctly first without all this then do this process. Once this is setup and working you can add any number of scenes and corresponding Cal events without having to touch the automation. Do not overlap cal events and do not include standard scene in the Calander.
Go and Automate…
alias: Event Schedule
description: "Calendar Event Automation "
mode: single
triggers:
- event: start
offset: "0:0:0"
entity_id: calendar.events
id: Event Start
trigger: calendar
- event: end
offset: "0:0:0"
entity_id: calendar.events
id: Event End
trigger: calendar
conditions: []
actions:
- variables:
scene_name: "{{ trigger.calendar_event.summary }}"
scene_entity: "{{ 'scene.' + scene_name }}"
scene_entities_to_turn_off: "{{ state_attr(scene_entity, 'entity_id') }}"
- choose:
- conditions:
- condition: trigger
id:
- Event Start
sequence:
- data: {}
target:
entity_id: |
{{ state_attr('scene.standard_outside_scene', 'entity_id') }}
action: light.turn_off
- delay:
hours: 0
minutes: 0
seconds: 10
milliseconds: 0
- target:
entity_id: "{{ scene_entity }}"
metadata: {}
enabled: true
action: scene.turn_on
- conditions:
- condition: trigger
id:
- Event End
sequence:
- target:
entity_id: "{{ scene_entities_to_turn_off }}"
data: {}
action: light.turn_off
- delay:
hours: 0
minutes: 0
seconds: 10
milliseconds: 0
- data: {}
target:
entity_id: scene.standard_outside_scene
enabled: true
action: scene.turn_on
enabled: true