Build a Home Assistant: the light fantastic

By PJ Evans. Posted

Last month we got our Home Assistant set up and ready to work. Now it’s time to look at
home automation’s most popular feature; lighting control. Automating lights has never
been easier and cheaper. It’s also a great way to find out about some of Home Assistant’s
great features such as scenes or triggers.

We’re going to use the Ikea Trådfri range in this tutorial, but you can also use other compatible ranges such as Philips Hue. So, let’s brighten up our day (and night) without the need for boring light switches.

Step 01: Install your smart light system

Like many ‘smart’ light systems, Trådfri uses direct radio communication using a protocol called ZigBee. To control the lights from a computer, the easiest approach is to add a suitable gateway. These are normally inexpensive and are often cross-compatible (as with Trådfri and Hue). Once installed, the Ikea Trådfri Gateway will allow not only your smartphone to control lights but also Home Assistant. Typically they need a wired connection, so it’s a good idea to have your Home Assistant Raspberry Pi computer wired on the same network switch for reliability. Follow the supplied instructions and get set up before moving on.

Step 02: Pair up your lights

It’s far easier to get your new smart light setup running as a closed system first: it makes discovery easier later on. We’re going to cover a number of different scenarios such as an automated porch light and a group of lights that can act as one. These will all need setting up in the Trådfri smartphone app so they are recognised by the gateway. You can also group them if you want to, as that will be helpful later and save some work. Don’t feel you have to follow our setup here – you can use a single light or as many as your home can take.

Step 03: Integrate!

There are a wide range of cards for setting up controls, and you can design your own

Once you’re happy your smart light gateway is running, log in to Home Assistant and go to Configuration, Server Controls, and then click ‘Restart’ under Server Management. This will cause Home Assistant to rescan the network for new devices. Luckily, Home Assistant is capable of spotting a Trådfri gateway automatically and you should get a notification that a new integration has been discovered. You can now enable the integration and Home Assistant will automatically add all your lovely new lights. Each light will be added as a device, something on your systems and an entity – a single thing that can be controlled.

Step 04: Nice name

Now Home Assistant is aware of your fancy new lights, it’s time to make things a bit more friendly. Each light now has an entity code in the system. These are normally in the form of type-dot-name, so as the device type is ‘light’, you’ll see entities such as ‘light.tradfri_bulb_1’ listed under Configuration - Entities. You can click on these to give them more meaningful names and place them in ‘areas’ such as the kitchen or lounge. Once you’re happy with all the names and places, you can add cards to the main control panel.

Step 05: Card-making

Last month, we configured the default dashboard ‘Overview’ and switched off automatic updates. If you did this, then (surprise!) controls for your new bulbs will already be available. Otherwise, click the three dots in the top-right-hand corner and then ‘Configure UI’. Now click the bottom right ‘+’ to add a new card. Choose the ‘Light’ card. Chances are, Home Assistant will have picked up your bulb and name. If not, click the arrow to select the entity you need. Click ‘Save’ to add your new card. You should now be able to tap the card to control your light and the slider to set brightness (if your light supports it).

Step 06: Add in an add-in

For one of our setups we want many lights to be controllable together, including dimming. This is a bit trickier and not something we can do in the interface – we’ll have to edit a configuration file. To do that, we need to get console access and that means installing an add-in. Add-ins extend Home Assistant’s core functionality. Click on ‘Supervisor’, then ‘Add-on Store’ to see what’s available. Search for ‘terminal’ and install the ‘Terminal & SSH’ add-on. Once installed, ‘Terminal’ will appear on your sidebar. Click it to get a command line prompt. Also you now have SSH access to your installation if you want.

Step 07: Configure it out

The add-on store extends your server in all kinds of ways

To control our multiple lights with one control, we need to create a single ‘virtual’ light. This gives us the chance to familiarise ourselves with Home Assistant’s configuration files. Click on the Terminal add-on and then enter

cd config
. In this directory are all the configuration files for the system. To make our virtual light we need to edit configuration.yaml. YAML files are text files that use indentation and special characters to convey meaning. The main file is configuration.yaml. Any changes made there require Home Assistant to be restarted. Others, such as groups, can be dynamically reloaded from Configuration – Server Controls.

Step 08: A little light typing

The virtual light group is a little special, so it has to be created in the main configuration.yaml. Edit the configuration file as follows:

nano ~/config/configuration.yaml
Now enter in the example shown at the end of the file. You’ll need to change the entities to match your installation and of course you can change the name to anything you wish. If you add any further groups, you should add them under the existing ‘light’ section. Save the file with CTRL+X and then restart Home Assistant to pick up the changes. You should now be able to add a new card to the dashboard which will use your new bulb and control everything in sync.

light:
  - platform: group
     name: Kitchen Lights
     entities:
         - light.tradfri_bulb_1
         - light.tradfri_bulb_2

Step 09: All in good time

Let’s create an automation to switch the kitchen lights off at 11pm. Click ‘Configuration’ then ‘Automations’. This is the most powerful area of Home Assistant and we’ll go more in- depth next month. For now, click the ‘+’ icon and, when prompted, enter ‘turn off the kitchen lights’, then ‘create automation’. This will set up a basic automation for you. Go to ‘Triggers’, select a type of ‘Time’, and enter ‘23:00’. Make sure all your lights are selected under ‘Actions’ and then click the save icon (yes, it’s a floppy disk). Now your kitchen lights will switch themselves off every night at 11pm. Can you get them to switch on?

Step 10: Triggered

A key part of automations is the ‘trigger’: the event or change that causes an automation to run. Triggers are fed by sensors, devices connected to the system that feed it information, rather than being something it controls. Examples are temperature, humidity, movement (PIR sensors), and buttons. Home Assistant automatically adds the sun (!) as an input and knows when the sun rises and sets, so automations can be triggered. If you have a porch light, this is a simple step and the language parser will accept ‘switch the porch light on when the sun sets’ and configure everything for you. Remember to ask it to switch it off again!

Step 11: Setting the scene

Another way of controlling groups is to use ‘Scenes’. You’ll find the scenes editor under Configuration. A ‘scene’ is a group of devices that you would want to group together in a particular way. For example, you might want a single button that turned off the main light, turned on the mood lights and started playing music on your smart speaker. This works by adding entities together in a scene and then creating a button in the dashboard to trigger that scene. You can also have the scene triggered using automations; imagine having the lights and music come on when you entered the house by using your smartphone app as a trigger.

Step 12: Notifications

Now it may not be very useful to know that a light has come on, but it does make for an interesting example of how notifications work. Let’s say you wanted an alert when the porch light switches on. You firstly need to add a notification provider to configuration.yaml. There’s an example here for one of our favourite services, Pushover.net:

notify:
    - name: pushover
       platform: pushover
       api_key: <YOUR API KEY GOES HERE>
       api_key: <YOUR USER KEY GOES HERE>
Once added (and with the service restarted), you can add notifications as an action on any automation. There is support for virtually every popular notification system out there.

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.