Are there improvement for Christmas lights automation

Trying to learn HA automation. Most of my automation to this point has been in AppDeamon scripts. So since it getting close to Christmas, I thought I would give it a try with that as the theme. I thought I would post it to see if there is a better mousetrap. “Better” being relative to what end. More reliable? Smaller? Easier to read? While do my searches I learned to be careful to watch the dates on posts and examples. Lots of improvements over the years mean something may work, but is it the best practice. For example, after the -service line in an automation if you just have an entity then you don’t need data/data_template. But anything else you do, so why not add data/data_template even if you don’t need to for “best practices”.
I created two different timers. One runs all night and the other shuts off in the middle of the night and turns back on for the morning.
So from the control panel, you can control the Start/End dates (not wired up yet). Turn the automation off/on and both timers state.
No one is going to hurt my feelings by pointing out some part as a stupid way of doing it. The point here is to learn.

######################################################################################
#I thought about adding a start/end date
#If I ever do all it has to do is control the run automation switch (christmas_lights_run)
######################################################################################
input_datetime:
  start_christmas_lights:
    name: "Start Christmas Lights"
    has_date: true
    has_time: false
  end_christmas_lights:
    name: "End Christmas Lights"
    has_date: true
    has_time: false    
######################################################################################

    
######################################################################################
#One switch is control the automation and the other two are for the lights
######################################################################################
input_boolean:
  christmas_lights_run:
    name: "Christmas Lights Run Automations"
  christmas_lights_all_night:
    name: "Christmas Lights All Night"
  christmas_lights_night_morning:
    name: "Christmas Lights Night then Morning"
######################################################################################
    

automation:
####################################################################################
#These 4 automation is what actually controls the devices.
#Add any other devices to the both the correct on/off action
#They are contoled via the two input boolean switches
####################################################################################
- alias: christmas_lights_all_night_switch_on                  
  trigger:
    - platform: state
      entity_id: input_boolean.christmas_lights_all_night
      to: 'on'
  action:
    - service: light.turn_on
      data:
        entity_id: light.christmas_lights_outside
- alias: christmas_lights_night_morning_switch_on
  trigger:
    - platform: state
      entity_id: input_boolean.christmas_lights_night_morning
      to: 'on'
  action:
    - service: light.turn_on
      data:
        entity_id: light.christmas_lights_tree

- alias: christmas_lights_all_night_switch_off
  trigger:
    - platform: state
      entity_id: input_boolean.christmas_lights_all_night
      to: 'off'
  action:
    - service: light.turn_off
      data:
        entity_id: light.christmas_lights_outside
- alias: christmas_lights_night_morning_switch_off
  trigger:
    - platform: state
      entity_id: input_boolean.christmas_lights_night_morning
      to: 'off'
  action:
    - service: light.turn_off
      data:
        entity_id: light.christmas_lights_tree
###############################################################################################        

####################################################################################
#These 2 automation switch on/off the automation that actually controls the input_boolean
####################################################################################
- alias: christmas_lights_automations_on
  trigger:
    - platform: state
      entity_id: 'input_boolean.christmas_lights_run'
      to: 'on'
  action:
    - service: automation.turn_on
      data:
        entity_id: automation.christmas_lights_on_sunset
    - service: automation.turn_on
      data:
        entity_id: automation.christmas_lights_off_sunrise
    - service: automation.turn_on
      data:
        entity_id: automation.christmas_lights_off_midnight
    - service: automation.turn_on
      data:
        entity_id: automation.christmas_lights_on_atfour
        
- alias: christmas_lights_automations_off
  trigger:
    - platform: state
      entity_id: 'input_boolean.christmas_lights_run'
      to: 'off'
  action:
    - service: automation.turn_off
      data:
        entity_id: automation.christmas_lights_on_sunset
    - service: automation.turn_off
      data:
        entity_id: automation.christmas_lights_off_sunrise
    - service: automation.turn_off
      data:
        entity_id: automation.christmas_lights_off_midnight
    - service: automation.turn_off
      data:
        entity_id: automation.christmas_lights_on_atfour
###############################################################################################        
        
        
####################################################################################
#These automations switch on/off the input_boolean that controls the lights
####################################################################################
- alias: christmas_lights_on_sunset
  trigger:
    - platform: sun
      event: sunset
      offset: "+00:30:00"
  action:
    - service: input_boolean.turn_on
      data:
        entity_id: input_boolean.christmas_lights_all_night
    - service: input_boolean.turn_on
      data:
        entity_id: input_boolean.christmas_lights_night_morning
- alias: christmas_lights_off_sunrise
  trigger:
    - platform: sun
      event: sunrise
      offset: "-00:30:00"
  action:
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.christmas_lights_all_night
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.christmas_lights_night_morning
- alias: christmas_lights_off_midnight
  trigger:
    - platform: time
      at: "00:30:00"
  action:
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.christmas_lights_night_morning

- alias: christmas_lights_on_atfour
  trigger:
    - platform: time
      at: "04:00:00"
  action:
    - service: input_boolean.turn_on
      data:
        entity_id: input_boolean.christmas_lights_night_morning
###############################################################################################

I like what you have going here. I’m also relatively new to all this. I’m still in the “if it works, I’m happy” mode. Best practices have not necessarily been on my mind but I have been slowly trying to clean up my yaml lately.

For my Christmas lighting I have my automations for on and off. A morning and evening trigger for “on” and the morning and night trigger for “off”
Both of those automations are set to off so they won’t trigger.

Then I have a third and fourth automation that use the date/time sensor as a trigger to turn those automations on at midnight the day after thanksgiving (tradition at my house is to turn on Christmas lights the day after thanksgiving) and then off on the 5th of January.

I’m sure there’s a sexier way to do this but I like it.

The following can be condensed though:

To this:

action:
  - service: automation.turn_off
    data:
      entity_id:
      - automation.christmas_lights_on_sunset
      - automation.christmas_lights_off_sunrise
      - automation.christmas_lights_off_midnight
      - automation.christmas_lights_on_atfour

That at least saves you a few lines and is a little easier to read. Obviously this will work with the turn_on service call as well. This is how I have my automations turning on and off with the date/time sensor.

I also just noticed your commented out section.

That is basically what I’m doing.

1 Like

I use an input boolean ‘switch’ to enable holiday mode for my chrismas lights automation. Additionally, I turn off the christmas tree lights when no one is home based on bluetooth presence. We have a very well lit tree (1200W), so I feel safer when it isn’t left alone.

1 Like

This is a great idea. While it seems obvious since we probably all have automations for our house lights to turn off when we’re not home, I know I probably would’ve forgotten to add that switch (or light or whatever entity) to the script. Or just another automation that can turn on and off with the other seasonal automations.

I realise this is an old thread, but just thought I would share my solution to this problem … I wanted my Christmas lights to come on automatically from Nov 30th through to Jan 7th.

Can anyone suggest any improvements?

- id: Christmas automations
  alias: Christmas
# Trigger between 30th Nov and 7th Jan only
  trigger: 
    - platform: sun
      event: sunset

# if month is Jan, then we need to compare from last year
# if month is Dec, then we need to compare to next year
  condition:
    condition: template
    value_template: >-
      {% if mth == 1 %}
      {{ as_timestamp(now().date())  >= as_timestamp((now().strftime('%Y') | int -1 ) ~ "-11-30") 
         and as_timestamp(now().date()) <= as_timestamp((now().strftime('%Y') | int) ~ "-01-07") }}
      {% else %}
      {{ as_timestamp(now().date())  >= as_timestamp((now().strftime('%Y') | int ) ~ "-11-30") 
         and as_timestamp(now().date()) <= as_timestamp((now().strftime('%Y') | int + 1) ~ "-01-07") }}
      {%endif%}

      
  action:
    service: homeassistant.turn_on
    entity_id: group.christmas_lights

It turns out I missed setting mth variable in my template, it should have looked like this

  value_template: >-
    {% set mth = int(now().strftime('%m')) %} {% if mth == 1 %} {{
    as_timestamp(now().date()) >= as_timestamp((now().strftime('%Y') | int-1 ) ~
    "-11-30") and as_timestamp(now().date()) <=
    as_timestamp((now().strftime('%Y') | int) ~ "-01-07") }} {% else %} {{
    as_timestamp(now().date()) >= as_timestamp((now().strftime('%Y') | int ) ~
    "-11-30") and as_timestamp(now().date()) <=
    as_timestamp((now().strftime('%Y') | int+1) ~ "-01-07") }} {%endif%}

Is there a reason why you wouldn’t simply use this:

{% set n = now() %}
{% set d,m = n.day,n.month %}
{{ (m == 11 and d > 29) or (m == 12) or (m == 1 and d < 8) }}

?

That is a lot better, thanks!!