Ok, so here’s one of the automations that I think is pretty fun-- my neighbours might disagree though. This is how I use Home Assistant to automatically trigger a goal celebration whenever the Winnipeg Jets score!

So, this relies on the Goalfeed component that is in the 0.63 release of Home Assistant, which requires an account with goalfeed.ca (full disclosure, I wrote and operate this service and there's a $1.99/month subscription to cover server cost and external services etc). Once you’ve got the account and your credentials are in your configuration, Home Assistant will fire events every time an NHL or MLB team scores. These events can be used to trigger whatever automation you want.

Now, the way this component works is that it fires an event called ‘goal’ for every goal or run scored, the event has some event data that is included which basically just identifies which team scored. Ultimately, the automation is going to look like this:

- alias: 'Jets Goal'
  hide_entity: true
  trigger:
    platform: event
    event_type: goal
    event_data:
      team_name: "Winnipeg Jets"
  action:
  - delay: "00:00:{{ states.input_number.goalfeed.state | int }}"
  - service: homeassistant.turn_on
    entity_id: input_boolean.goaling
  - service: media_player.play_media
    data:
      entity_id: media_player.everywhere
      media_content_id: https://hassbian.local:8123/local/audio/goalhorn1718.mp3
      media_content_type: audio/mp3
  - service: script.turn_on
    entity_id: script.goal_start

Since we don’t want to trigger this automation every single time any team scores, we’ve got to specify the event data that we want to match in the automation trigger. You’ll notice there’s a lot going on in the action of this automation.

First, in the action, I have this delay instruction with this template:

  - delay: "00:00:{{ states.input_number.goalfeed.state | int }}"

I’ve added an input_number entity in my config like this:

input_number:
  goalfeed:
    name: Goalfeed Delay
    initial: 30
    min: 0
    max: 180
    step: 1

The reason for this is that the goalfeed events usually come in really quickly. I watch most games online, and depending on the source and my connection, the time between when the event comes in and when I see the goal sometimes requires a bit of tweaking, so having a nice little slider in the front end to configure this is key.

The next action item is setting an input_boolean (which I also added in my main config). I just set this to true to represent the fact that the goal light script is running. I’ll show you how I use this in the second automation.

The third item is the call to my media_player entity, which is a group of google homes/chrome cast audios that plays an mp3 of the Jets goal horn throughout the house.

The last item is turns on a script, which basically makes the light show work. I’ll get to that in a minute, but let’s go back to that input boolean for a second.

The input boolean that we turn on in the first automation we use as a trigger for a second automation that ends the goal celebration. That automation looks like this:

- alias: 'Turn off goal'
  trigger:
    platform: state
    entity_id: input_boolean.goaling
    to: 'on'
    for:
      seconds: 30
  action:
  - service: media_player.stop_media
    data:
      entity_id: media_player.everywhere
  - service: script.turn_on
    entity_id: script.goal_stop
  - service: homeassistant.turn_off
    entity_id: input_boolean.goaling

This is pretty straightforward-- when the input_boolean has been ‘on’ for 30 seconds, this automation runs, it runs a stop script and sets the input_boolean back to ‘off’.

Ok, now let’s take a look at the scripts for the light sequence.

goal_start:
  sequence:
  - service: scene.turn_on
    entity_id: scene.all_dark
  - service: script.turn_on
    entity_id: script.goal_repeat

goal_loop:
  sequence:
    - condition: state
      entity_id: input_boolean.goaling
      state: 'on'
    - alias: swon
      service: homeassistant.turn_on
      data:
        entity_id: light.goallight
        rgb_color: [255, 0, 0]
        brightness: 255
        transition: 0.25
    - delay:
        seconds: 0.25
    - condition: state
      entity_id: input_boolean.goaling
      state: 'on'
    - alias: swoff
      service: homeassistant.turn_off
      data:
        entity_id: light.goallight
        transition: 0.25
    - delay:
        seconds: 0.25
    - alias: loop
      service: script.turn_on
      data:
        entity_id: script.goal_repeat

goal_repeat:
  sequence:
    - condition: state
      entity_id: input_boolean.goaling
      state: 'on'
    - delay:
        seconds: 1.5
    - service: script.turn_on
      entity_id: script.goal_loop

goal_stop:
  sequence:
  - service: homeassistant.turn_off
    entity_id: script.goal_loop
  - service: homeassistant.turn_off
    entity_id: script.goal_repeat
  - service: homeassistant.turn_on
    entity_id: group.main_floor
  - service: light.turn_on
    entity_id: light.goallight
    data:
      brightness: 255
      rgb_color: [255, 172, 68]

So, there are four scripts here in my setup. The first is the start script. This handles turning off all of my non-colour changing lights and then starts the goal loop.

The goal loop consists of two scripts and the reason why have two is that we need two scripts in order to make the looping mechanism work-- the first script has our light sequence and then it triggers the second script, which basically just triggers the first script again.

There are two things that might be worth noting about the primary goal script, but I’ve gone into more detail on both of these things in other posts, so if you are curious, feel free to follow the links to those posts. The first is using a single light entity to control a group of hue bulbs-- I outline this a bit in this quick tip. The second is the use of the transition and delay instructions-- the need for a delay in this use of transition is covered a bit more in my post on building a sunrise alarm clock.

Feel free to comment with any questions that you might have!

This is a new automation in my set-up, but it’s one I’m pretty happy with, not only because I think it’s a pretty cool effect, but for me, it’s actually a real money-saver.

Here’s how I used Philips Hue and Home Assistant to build a natural coloured sunrise in my bedroom and save over $400/year--

How does this save me money?

I am addicted to snoozing on alarms in the morning.

In the mornings, I can either have a nice relaxing 30 minute walk into the office, or I can rush to the bus stop about 7 minutes away and catch an 10 minute bus. This choice getting left to be made by the sleepiest version of myself is no good-- sleepy Craig sucks. He’s a lazy piece of shit. He’d sell his first-born child for an extra 13 minutes of sleep in to morning (this is why we don’t have children).

Over time, taking the bus adds up. By a conservative estimate of the e-cash fare ($2.35, for now...), and saving 4 trips/week for 48 weeks, that’s $451.20 back in my pocket! Fuck yeah!

This automation has significantly cut down on how many times I hit snooze, and I also find that waking up is less jarring and I’m a lot less groggy as a result.

If you are a Home Assistant user and you have Philips Hue bulbs or some other coloured smart lighting system setup that supports transitions, I definitely recommend setting something like this up!

How does it work?

I’m going to assume that you’ve already got Home Assistant set up and working with your lights. If not, head over to homeassistant.io and give it a try. It’s really pretty easy to get up and running and the learning curve is not as steep as it looks, plus, there is a fantastic community that will provide you with support if you hit a snag.

Now, to implement this, we are going to be creating a Home Assistant script, which will be what run to create our sunrise effect, we’ll create a couple of entities to use in the front end so that we can easily configure when we want this to run, and then we’ll create the automation that will ultimate trigger the script at the desired time.

The script

If you’ve played around with Home Assistant a bit, you might have already got a few scripts set-up, or you might just have everything configured right in the actions of your automations. There are a couple of advantages to doing this sort of thing in a script-- the first is that scripts allow us to reuse the actions easily and then if we want to change the action, we only need to adjust it in one place. This is especially nice when we are building more involved multi-step actions like our sunrise.

Here’s what our sunrise script looks like-- It’s a long one, but I’ve got some comments there to give you an idea of what each block is doing and separated it into blocks for readability:

sunrise:
  sequence:
    # light 1 dark red to medium warm orange
    - service: homeassistant.turn_on
      data:
        entity_id: light.craigs_bedroom_lamp
        rgb_color: [98,19,0]
        brightness: 1
    - delay:
        seconds: 1.00
    - service: homeassistant.turn_on
      data:
        entity_id: light.craigs_bedroom_lamp
        rgb_color: [249,123,0]
        brightness: 103
        transition: 15
    - delay:
        seconds: 15.00

    # light 2 dark warm orange to medium warm orange
    - service: homeassistant.turn_on
      data:
        entity_id: light.jessicas_bedroom_lamp
        rgb_color: [249,123,0]
        brightness: 1
    - delay:
        seconds: 1.00
    - service: homeassistant.turn_on
      data:
        entity_id: light.jessicas_bedroom_lamp
        rgb_color: [249,123,0]
        brightness: 103
        transition: 15
    - delay:
        seconds: 15.00

    # group medium warm orange to full bright
    - service: homeassistant.turn_on
      data:
        entity_id: light.sunrise
        rgb_color: [255,178,67]
        brightness: 255
        transition: 30
    - delay:
        seconds: 30.00


    # turn one bulb overhead on lowest and fade to full brightness slowly
    - service: homeassistant.turn_on
      data:
        entity_id: light.bedroom_fan_1
        brightness: 1
    - delay:
        seconds: 1.00
    - service: homeassistant.turn_on
      data:
        entity_id: light.bedroom_fan_1
        brightness: 255
        transition: 60
    - delay:
        seconds: 60.00

    # turn on the second bulb overhead and fade to full brightness a bit faster
    - service: homeassistant.turn_on
      data:
        entity_id: light.bedroom_fan_2
        brightness: 1
    - delay:
        seconds: 1.00
    - service: homeassistant.turn_on
      data:
        entity_id: light.bedroom_fan_2
        brightness: 255
        transition: 30

    # no delay - also through put the corridor outside the room on
    - service: homeassistant.turn_on
      data:
        entity_id: light.upstairs_corridor
        brightness: 200
        transition: 5

There are a couple of things worth noting about this script. What makes this effect work well is the use of the light entities ‘transition’ attribute in combination with the ‘delay’ action in the script.

Typically, Home Assistant wants to perform actions quickly and then immediately move on to the next action. Normally, that’s exactly what we want, but in this case, it’s not. By setting the ‘transition’ in the data when we call the ‘homeassistant.turn_on’ service, we are telling Home Assistant how long it should take to transition from the lights current state to the new state. Now, we need Home Assistant to wait before performing the next step so we use the ‘delay’ action and set it to the same number as the number we have as our transition time.

I’ve played with the times and I found that for me, these are the durations that work best, but you can play with them to suit your preference.

The entities

Now, before we write our automation to trigger this, lets create some entities that we can use to configure this a bit easier.

I’ve got this set as two separate automations in my set-up. One for weekdays, and the other for weekends. This lets me have separate wake-up times that I don’t need to mess with too often.

Here’s the entities I’ve created:

input_boolean:
  weekday_sunrise:
  weekend_sunrise:

input_number:
  weekday_alarm_hour:
    name: Hour
    icon: mdi:timer
    min: 0
    max: 23
    step: 1
  weekday_alarm_minutes:
    name: Minutes
    icon: mdi:timer
    min: 0
    max: 59
    step: 5
  weekend_alarm_hour:
    name: Hour
    icon: mdi:timer
    min: 0
    max: 23
    step: 1
  weekend_alarm_minutes:
    name: Minutes
    icon: mdi:timer
    min: 0
    max: 59
    step: 5

sensor:
  - platform: template
    sensors:
      weekday_alarm_time:
        friendly_name: 'Time'
        value_template: '{{ "%02d:%02d" | format(states("input_number.weekday_alarm_hour") | int, states("input_number.weekday_alarm_minutes") | int) }}'
      weekend_alarm_time:
        friendly_name: 'Time'
        value_template: '{{ "%02d:%02d" | format(states("input_number.weekend_alarm_hour") | int, states("input_number.weekend_alarm_minutes") | int) }}'

Okay, whoa, whoa, whoa! What the fuck are those sensors? They look complicated.

Yeah, you can add those and not touch them, but if you want to know what they are doing, I’ll try to explain. If you don’t give a shit or already know, skip ahead

As you may or may not know, Home Assistant has support for a template engine called Jinja. This allows you to evaluate and manipulate data at execution. These sensors that we are creating here basically takes the data and instructions that we are giving it and evaluate it as the sensors state. This is a really powerful tool to have in your Home Assistant arsenal, and I’d highly recommend looking at the Home Assistant documentation and the Jinja documentation to get a better grasp on this.

I’ll try to breakdown this statement and what happens behind the scenes:

{{ "%02d:%02d" | format(states("input_number.weekday_alarm_hour") | int, states("input_number.weekday_alarm_minutes") | int) }}

We’ll start looking at this from the inside-out

{{ "%02d:%02d" | format(states("input_number.weekday_alarm_hour") | int, states("input_number.weekday_alarm_minutes") | int) }}

This item in bold is just getting the state of the entity that it’s being passed. In this case, it’s going to be a string of characters representing a number-- If you remember when we created this entity, from 0-23 representing the hour we want to wake up at. Let’s pretend that we’ve got this set to 6. So this is going to end up returning “6”, and our statement now essentially looks like this:

{{ "%02d:%02d" | format(“6” | int, states("input_number.weekday_alarm_minutes") | int) }}

Now, we’ve got the same thing happening to the other number entity.

{{ "%02d:%02d" | format(“6” | int, states("input_number.weekday_alarm_minutes") | int) }}

This represented minutes, so it’s going to be between 0-59. Let’s pretend we’ve got this set to 30. As you can probably guess, this is going to return “30”

{{ "%02d:%02d" | format(“6” | int, “30”| int) }}

Ok, now what is that line symbol ('|') we see in here? That’s a pipe symbol and in jinja, it’s the filter operator. Essentially, it takes what is on the left and filters it through what is on the right side.

{{ "%02d:%02d" | format(“6” | int, “30”| int) }}

In the case above, we are taking “6” and putting it though the int filter. This int filter basically says to take the characters on the left side and make them a number that the computer can use. So “6” | int becomes 6 and “30” | 30. Now our statement looks like this:

{{ "%02d:%02d" | format(6, 30) }}

Ok, that’s a bit easier to read now. Now we’ve just got some crazy characters, the filter with our numbers representing the time.

{{ "%02d:%02d" | format(6, 30) }}

So, the format filter means to apply Python string formatting. Python string formatting is a way for you to say “I want a string of characters that looks like this” and then give it some data and get a string that looks the way you asked it to.

The crazy string of characters represents how we want the resulting string to look and the numbers in the brackets are extra data (called parameters) that we want to be formatted in this way.

PyFormat.info has some good information on all the ways you can format a string, but what you need to know about this is that the ‘%’ sign means that we are going to put