A smart home Christmas

With all the home automation going on in my house it was inevitable that I would include the Christmas decorations…

Obviously the first thing to do was to plug the Christmas tree lights into a smart plug. Surely everyone into home automation does that!? I have a spare 433MHz socket that I use for trying things out so that was pressed into service for the tree.

I also have a lamp in my living room, and I thought it would be nice to have it cycle through suitably festive colours while the Christmas tree was on. The lamp’s in the shape of a penguin, so it’s already halfway to being appropriately seasonal and it already has a Hue bulb in it.

I started with the basics. I wanted a switch to turn “Christmas mode” on and off, so that was an input_boolean switch in Home Assistant:

    name: Christmas
    initial: off

Then I set up an automation to run when the switch is turned on:

- alias: 'Xmas light start'
    - platform: state
      entity_id: input_boolean.christmas
      to: "on"
    - service: automation.turn_on
      entity_id: automation.xmas_lights
    - service: switch.turn_on
      entity_id: switch.spare

That just turns on the spare plug that controls the Christmas tree and enables another automation called xmas_lights. I’ll come back to that below.

When the switch is turned off, another automation turns off the xmas_lights automation and resets the lamp to a default colour before turning it off:

- alias: 'Xmas light stop'
    - platform: state
      entity_id: input_boolean.christmas
      to: "off"
    - service: automation.turn_off
      entity_id: automation.xmas_lights
    - service: light.turn_on
        entity_id: light.penguin
        color_temp: 400
    - service: light.turn_off
      entity_id: light.penguin
    - service: switch.turn_off
      entity_id: switch.spare    

The key element is the xmas_lights automation itself. After attempting some YAML to get the lamp to cycle through some colours I decided it would be easier to do in Python. I enabled the python script component in my main configuration file:


Then I set up the xmas_lights automation to run a script called xmas.py every 10 seconds:

- alias: 'Xmas Lights'
  initial_state: "off"  
    - platform: time
      seconds: '/10'
    - service: python_script.xmas

Note that the initial state is off and the automation is enabled and disabled with the automations behind the input boolean switch. It will only be running when the switch is on.

Now I just needed to create the python_scripts subdirectory under the main configuration directory and add the xmas.py script:

last = hass.states.get('sensor.xmas')

if last is None:
    light = 0
    light = int(last.state)
transition = 8
brightness = 80

colors = ['crimson', 'darkgreen', 'crimson', 'blue']

color = colors[light]
light = light + 1
if light >= len(colors):
    light = 0

data = {
        "entity_id": "light.penguin", 
        "brightness": brightness, 
        "transition": transition,
        "color_name": color
hass.services.call('light', 'turn_on', data)
hass.states.set('sensor.xmas', light)

It uses a sensor to keep track of the state between invocations. That state value is an index into an array of colours – I’m using colour names but it could just as easily be an array of RGB values or any of the other ways of specifying the light colour.

Each time the script is called it changes the light to the next colour and increments the index ready for the next time, cycling back to the beginning when it reaches the end of the array.

With the script running every 10s I found that using a transition time of 8s worked well to create gradual transitions. I also decided to alternate between blue and green in between the reds. That colour scheme more or less matches my tree lights – the transition between red and blue goes through purple to complete the set.

One of the great things about using Python scripts is that you don’t have to restart Home Assistant or reload automations when you make changes. Home Assistant will always pick up the latest version of the script. That was really handy when I was experimenting with the colours.

The last touch was to ensure that I could control it all through my Echo, so in my customise.yaml I allowed the switch to be picked up by the emulated hue component:

  emulated_hue_hidden: false

And now all I have to do is say “Alexa, turn Christmas on”…


Although this is quite a niche use it gave me an opportunity to try out Python script automations in Home Assistant and the techniques could easily be applied elsewhere.

And with that I wish you all a Merry Christmas! I’ll be taking a couple of weeks off from blogging and be back in January.

in Home Automation

Add a Comment

Your email address will not be published. All comments will be reviewed.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts