While we could just as well have tied the MQTT topics to a device for direct control, I wanted to have the PyPortal use its own topic for the buttons. This will allow us to create an automation that it triggered when the button is pressed. So we get to use the Home Assistant UI rather than having to change the code on the PyPortal if we want other things to happen. Using Automations also allows us to do more than just one thing when we press the button.
For this example, we will be using the buttons to control a LIFX light along with a Google Home Mini. We will also go through using the buttons with conditions to activate one script based on what time of day it is.
Creating the Toggle Button Automations
To handle button1 we will be creating an Automation for turning on and one for switching to off. This will give us more control over what each state will do so that it can be more than just a light switch.
|
entity_id: light.lamp brightness_pct: 100 color_temp: 250
- entity_id tells the action what light to turn on.
- brightness_pct sets the light level to 100%.
- color_temp sets the light to a lukewarm white
When finished, click the save icon at the bottom right of the browser.
Now we will create another Automation for PyPortal Button 1 Off so that each state of Button1 is covered. So just like before, create a new Automation.
|
entity_id: light.lamp transition: 4
- entity_id tells the action what light to turn on.
- transition will make it so the light will fade to off in 4 seconds.
When finished, click the save icon at the bottom right of the browser.
Automation for Button 2 press
This will handle what happens when the state of entity binary_sensor.button2 changes from off to on. Once triggered this Automation will fade the light on slowly and say "Good Morning" if pressed before 9AM.
|
entity_id: light.lamp brightness_pct: 100 color_temp: 250 transition: 60
To this we are going to add a condition in the Actions section. If this condition is true, than the following Actions will run or the Automation ends if the condition is false. Basically the light will fade from off to on when Button2 is pressed, but the next action will only take place if the time is between 4am and 9am.
|
entity_id: 'media_player.living_room_speaker' message: Good morning, I hope your day rocks!
So what tts.google_translate_say does is to convert the text from our message into sound and then send it to be played on the Google Home speaker that is selected by entity_id.
When finished, click the save icon at the bottom right of the browser.
Automation for Long Press of Button 2
This will handle what happens when the state of entity binary_sensor.button2 changes from off to on for more than 2 seconds. If this Automation is triggered, it will start a Party Mode.
|
Conditions
For this Automation, we will be adding a Condition that will need to be TRUE before the Automation can be triggered. For this example, we only want to access Party Mode if the sun has gone down.
Now our Automation can only be triggered if it is after the Sun has gone down. |
Add the Actions
Now we are going to add our actions to start Party Mode. This will include setting the LIFX light to cycle through colors and have the Google Home speaker play music from an internet radio station.
|
brightness: '50' entity_id: light.lamp period: '2' power_on: 'true'
The light.lifx_effect_colorloop is a service that is made just for LIFX bulbs though there is a similar one for HUE lights as well. If you are using a generic light, you can use the service light.on and effect: colorloop for the Service data.
Now we will get our Google Home speaker to play music.
|
media_content_id: 'http://knhc-ice.streamguys1.com/live' entity_id: media_player.living_room_speaker media_content_type: music
The media_player.play_media lets you send media files to a connected media device like our Google Home speaker. For this example we are using the internet radio link for Seattle's student run radio station C89.5 as the media source, but you can link this to a music file or any other streaming music link.
When finished, click the save icon at the bottom right of the browser.