It’s finally the weekend and you are ready for the big night in.
Pizza, popcorn and a great movie are a superb way to relax, particularly when it is cold and dark during the winter. A great TV and good quality sound certainly gets us one step closer to the cinema, but what about the lighting?
The icing on the cake for any home theatre system is the lighting, whether it be a simple TV and cosy sofa or a full scale projector and Dolby surround.
In this tutorial we will learn how to automatically dim the lights when we begin the playback of media within a Home Assistant media player entity.
We will also add support to brighten the light when the media player is paused or stopped. You won’t be stumbling around in the dark to goto the bathroom!
One thing I love about Home Assistant is that you can blend creativity and technology.
We will cover the basic configuration with just a single lamp entity. However once you understand this basic configuration, it is very easy to apply it to many different lights. You can be as creative as you like with your own scenes!
So without any further delay, lights camera, action!
Table of Contents
- Adding a media player
- Setting the scene
- Completed configuration
For this tutorial we will assume that you have Home Assistant up and running already. A very basic knowledge of configuration.yaml would also be advantageous, but we will cover as much detail as possible.
You will also need at least one light entity configured. It does not matter how the light communicates with your Home Assistant server, so long as it appears Home Assistant sees it as an entity.
You can check your entities by clicking developer in the sidebar and selecting the states tab. At the top of the list there is a text box where you can enter a filter.
Adding a media player
This process should work with any media player that you add to Home Assistant. Integrations are device specific, however there is a universal option for Chromecast devices.
Therefore in this tutorial we will focus on using the Chromecast media player entity. However you can use a different media player entity if you like.
Chromecast devices on your network will be discovered by Home Assistant and are accessible from the configuration menu. Open configuration and click integrations.
You should see Google Cast within your list of integrations.
Go ahead and click on it to bring up the list of detected Google Cast devices. Here we can see the Sony TV in my living room.
Go ahead and click your device to bring up the device specific page. If your media player is not already appearing in your overview then you can select add all device entities to lovelace to create a card.
At this stage you should either have a media player entity configured for Google Chrome, or you can use a different media player entity if you have configured one specifically for your device.
Setting the scene
As I mentioned previously we will keep things simple in this tutorial and just use a single lamp. However you can be as creative as you like with your own scenes!
I would recommend that you start with one light until it functions as expected. Once you know everything works, you can add your additional light entities.
For this demonstration we will add two scenes, one for the lights at full brightness and another to dim the lights. When we play something in our media player we will activate the scene that dims the lights. When we stop or pause, we will activate the scene for full brightness.
First we will need to open up our configuration.yaml file. You can edit it with a text editor if you prefer, but we will use the file editor. Click on file editor and click your configuration.yaml file.
If you do not see the file editor option in the sidebar, you need to install the file editor add-on. Click the Hass.io option in the sidebar and then go to the add-on store tab. From there you can install the file editor.
Add a scene for dim lights
Now lets begin writing our first scene by adding the following code to our configuration.yaml file. This line of code creates a new scene called “lights dim.”
scene: - name: lights dim
Don’t forget that YAML files are very sensitive to the formatting. You must make sure the tabs/spacing are correct or it will produce a syntax error.
If you are having problems getting things to work, the most likely cause is the formatting of the YAML file so double-check that it is all correct.
Next we need to include a light entity. For this example I am using a lamp in my living room called light.treelamp but you should choose your own lamp.
scene: - name: lights dim entities: light.treelamp:
If you are unsure on the name of your light, simply enter “light” into the search entity box in the file editor. It will present you with a list of your light entities. You can then click the entity in the search result and it will copy it to your YAML file.
Next we can add the configuration for the lamp. We will set the state to on so that the lamp switches on and we will set the brightness to 30%. You can of course choose whatever brightness you prefer.
scene: - name: lights dim entities: light.treelamp: state: on brightness_pct: 30
For simplicity I will stick to using one light in this example. However if you wish to add more lights, the code will look something like this. Make sure that your tabs/spacing match this example.
scene: - name: lights dim entities: light.treelamp: state: on brightness_pct: 30 light.anotherlamp: state: on brightness_pct: 30 light.yetanotherlamp: state: on brightness_pct: 30
Add a scene for bright lights
Now that we have completed our scene for dim lights, we can create one for the lights at full brightness.
We can copy and paste the first scene, but change the name to “lights bright” and set all of the brightnesses to 100%. Now our YAML file looks something like this.
scene: - name: lights dim entities: light.treelamp: state: on brightness_pct: 30 - name: lights bright entities: light.treelamp: state: on brightness_pct: 100
Note that the standard brightness parameter should be set between 0 and 255 as this is the number of steps in brightness that Home Assistant supports. The parameter brightness_pct allows us to specify the brightness in percent instead.
With our scenes complete it is time to create an automation. This is the part of our YAML file that will tell Home Assistant to activate the scene depending on the state of the media player.
Dim the lights
First let’s add the following line of code to our YAML file.
automation: - alias: "media playing"
Next we need to add a trigger and action. The trigger is the event that will cause the action to occur.
In this case when the media player changes from “idle” to “playing” it means that the media has started playing. When this happens we want our scene “lights dim” to be activated.
First we will specify the trigger using the platform state. This means the trigger will occur when something changes state, in this case when the media player changes to the state playing.
automation: - alias: "media playing" trigger: - platform: state
You can check the current state of any entity under the states tab in the developer menu accessible from the sidebar. This is useful if you need to find out the state to enter into your YAML file.
Next we can add the name of the media player, the entity_id. If you are unsure you can use the search entity feature and search for “media_player.”
In my case the entity is called media_player.kd_65xe8596 but you should use your own entity_id.
We also need to specify the state change that will trigger our automation. We want the action to occur when the state changes to playing therefore we can go ahead and add the following to our code.
automation: - alias: "media playing" trigger: - platform: state entity_id: media_player.kd_65xe8596 to: 'playing'
Next we need to add the action. We need to set the service scene.turn_on as we want to activate the scene to dim the lights.
We also need to specify the entity_id which is the name of the scene. Note that the space in the name lights dim is replaced with an underscore, lights_dim.
automation: - alias: "media playing" trigger: - platform: state entity_id: media_player.kd_65xe8596 to: 'playing' action: service: scene.turn_on entity_id: scene.lights_dim
Brighten the lights
Now that we have the automation in place to dim the lights, we need to add the automation to make the lights bright again when the media is stopped.
We can copy the first automation but we will rename it to “media stopped.” We also need it to occur for two states, pause and off, so that the lights brighten if the media is paused or the media player is stopped and turned off. To do this we will create two instances of platform with the to parameter set to “paused” and “off.”
Lastly we want to change the entity_id in the action section to scene.lights_bright.
Our completed automation section should now look a little something like this.
automation: - alias: "media playing" trigger: - platform: state entity_id: media_player.kd_65xe8596 to: 'playing' action: service: scene.turn_on entity_id: scene.lights_dim - alias: "media stopped" trigger: - platform: state entity_id: media_player.kd_65xe8596 to: 'off' - platform: state entity_id: media_player.kd_65xe8596 to: 'paused' action: service: scene.turn_on entity_id: scene.lights_bright
Ok we are done, congratulations! Now that you have all of the code written, it should look something like this.
scene: - name: lights dim entities: light.treelamp: state: on brightness_pct: 30 - name: lights bright entities: light.treelamp: state: on brightness_pct: 100 automation: - alias: "media playing" trigger: - platform: state entity_id: media_player.kd_65xe8596 to: 'playing' action: service: scene.turn_on entity_id: scene.lights_dim - alias: "media stopped" trigger: - platform: state entity_id: media_player.kd_65xe8596 to: 'off' - platform: state entity_id: media_player.kd_65xe8596 to: 'paused' action: service: scene.turn_on entity_id: scene.lights_bright
Next we need to save our YAML file, then restart Home Assistant. Once the reboot has completed, go ahead and give your new movie lights a whirl!
That’s it! You should now have the beginning of an awesome home theatre lighting configuration.
Next you can get creative, add some more lights and perhaps even introduce some colour changing! I hope you found this tutorial useful, please feel free to take a look at some of my other cool tutorials!