Setting up scripts and automations
Okay tough guy, are you’re frontend in a better shape and you are now ready for some extra laziness in your life? Well here’s some pointers to get you in right direction.
There are multiple ways to set scripts and automations up, I’m just covering basics of it so you can hopefully understand and make great stuff for your castle.
Okay for starters we need to know what we are gonna to make. It can be anything from turning all the lights to red and put on your’e A-game playlist on the stereo to get you better half in a jazzy mood or lock your house down, maybe both perhaps? As said earlier, only your creativity stops you here, hopefully.. I will go easy on you for now.
Setting up your first script
First thing we need to check is if scripts are enabled in configuration.yaml. Easiest way to find out is to search after «script:» and make and include of a new file
(as mentioned earlier, this gives you more overview of your application).
script: !include scripts.yaml
Allrighy, then we are one step closer to our first script. I’m now going to show you an example of what I’ve used several times to make peace in the house (run a YouTube video via ChromeCaster). I have a daughter at the age of two soon so this feature is still quite handy even though its been the same video over and over and over…. This feature is extra awsome if you can set your TV to turn on when it detects signal from the HDMI and change to right source.
This file should be pretty empty if this is your first script, so this is what I’ve implemented:
youtube_fantorangen: alias: Make your'e kids take a timeout sequence: - service: media_extractor.play_media entity_id: media_player.chromecast data: media_content_id: "REPLACE_WITH_YOUTUBE_URL" media_content_type: video/youtube
Ok cool, but how does it work?
I will try to explain how this is working line by line:
youtube_fantorangen – name of the script
alias – a description of what the script does (not required)
sequence – starting to define what the script is supposed to do
service – what service we want to call, here I’m using Media Extractor which is the one to get the video from YouTube and send it to the ChromeCast.
entity_id – what entity we want to use (still the ChromeCast)
data – what we are going to send to the entity, here were saying that its a youtube video and a specific link
So I can start use it now?
You may need to restart your home assistant first to get access to the script, and then you should be ready to start using the script.
How do I use it?
There is multiple ways to start the script. I’m using a custom card called Mini Media Player which is very cool and let you easily set up a card with your media players (ChromeCast, Google Home, Spotify and so on). It’s worth a visit and installation of the card.
- type: custom:mini-media-player entity: media_player.chromecast artwork: cover icon: mdi:google-chrome shortcuts: columns: 2 # Max buttons per row buttons: # Start predefined playlist - icon: mdi:cat type: script id: script.youtube_fantorangen
In this card you get access to the media player device info, toggle function (on/off), media controls, and you can set buttons for shortcuts. In this situation I’ve set a shortcut for my youtube_fantorangen script. And voila, a fast way to get your kids quiet in a rush! :D
Next step, Automations
Okay hopefully you have gotten a hang of how the script can be used, but’s let set up a insanely and unlikely scenario where your kids starting to cry every evening at 16:30 and accidentally sitting in front of the television (I really really really hope that’s not the case for anyone, but ok let’s continue).
Set up the automations in configuration.yaml
This process is literally the same as we did for the scripts.yaml file earlier, if it isn’t already in place. just repeat the same process and add automation: in the configuration file. And yes please create a seperate file for this aswell.
Our first automation
Ok so we are now done with the setup for a long long time, now we can focus on the good stuff. Let’s just get started by throwing a example in the air:
- alias: Running youtube video every day trigger: platform: time at: "16:30:00" action: service: script.youtube_fantorangen
Yes this snippet is kindof straight forward, we are setting a trigger based on time and setting a time (24 hours format) and saying, HEY start this script. Cool? I would say yes.
I repeat, Thats it!
Hopefully you have gotten some more overview of how scripts and autmations works in Homeassistant and next time we can maybe go further in on other fun components we can use in this system!