Schedule light color based on holidays

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 
3 Likes

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

1 Like

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.

2 Likes

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?