Imagine you had google notify you if you left a light on for too long or let you know if someone has left the back door open. You can do all of this with text to speech and home assistant.
Home assistant is a free and open source home automation system that allows you to create your own automation and routines by writing a few lines of code. You don’t need to be a developer to be able to write automations, you just need a google mini or google home and pair it to home assistant. In the integrations page under Configuration look for Google Cast, this should find any Google device within your network like a Google Home Mini or a Chromecast.
Table of Contents
What you need to do this project
First step you will need to choose some hardware on which you can install home assistant. If you are just starting out we recommend a Raspberry Pi 4 check latest price on Amazon US or UK.
If you are looking to future proof you can look into a Nuc i7 or i9, check the prices on Amazon US or UK.
I have opted to virtualise my installation on a Qnap Nas latest price on Amazon US or UK, this is working fine for me and I don’t need a dedicated piece of hardware, to give you some context a NAS (network attached storage) is a always on computer can can accomplish many tasks, like file sharing, CCTV etc.
You will need a google mini or google home, they are normally on sale during the holidays but quite inexpensive.
Automation Breakdown
With automation you need to think carefully and give clear and precise intrustrions to the software, as with any piece of software it will be as good as the logic you input.
Developers use something called pseudocode, which is an informal language intended for a human to read and not a set of instructions for a machine. This will give us a clear plan on what we want to do.
An automation in home assistant is split into these three parts (Triggers, Conditions and Actions):
Triggers
A trigger is the event that starts an automation, this is the first thing you will need to consider and get right !
What is an event ? Well one event could be at sun set or at sun rise, you would need to setup your location so that home assistant knows where sunset is at your location. You can use other smart devices in your home to trigger an event like a light bulb.
A smart light switch is a simple example it has two status either ON (1) or OFF (0).
So we can trigger an automation when the light is ON, this is what I will show you later how to do.
Conditions
A condition is an optional part of an automation rule that prevents an action from happening when triggered. The condition looks at the current state of the system and can’t observe a change like a trigger.
Example: A trigger is able to find out that a switch went from OFF to ON, while a condition can only see the current state of the switch.
If you had multiple conditions they all need to be true by default. For example switch is ON and someone is home, then notify me.
You can add OR conditions and NOT conditions to customise your automation.
Numeric conditions ensure that the automation runs only within a range, you can use the keywords below and above to define the range.
Example: when the temperature is above 17 and below 25.
Sun Condition
The sun can be used as a condition on the automation not only as a trigger. The entity id is sun.sun and it has various states: above_horizon and below_horizion.
If you want to be specific you can specify the sun elevation with an offset before or after. This is when you want lights to turn on but it is either too dark already that you end up turning it on manually which defeats the point or the light turns on too soon.
By adjusting the elevation you get it just right!
Time Condition
This is to control the automation by looking at what day of the week it is and what time it is. Key words are after, before and weekday.
Zone Condition
This is useful if you have a device tracker with GPS coordinates, like a dog collar or a car and you are checking it is home for example.
Actions
This is the part of the automation that does something after it has been triggered and meets all of the conditions if any.
You would need to specify the entity_id and any extra parameters that you want to set, light colour or brightness of a light bulb.
The Code!
In this section I’ll give you an example of some automations that I have in my automation.yaml file.
Notify me when a light in the toilet has been on for more than 30 mins.
- id: 'loo_lights_notification'
alias: Turn Loo Lights off Notification
trigger:
platform: state
entity_id: light.downstairs_loo
from: 'off'
to: 'on'
for: '00:00:05' # 5 seconds
action:
- service: tts.google_say
data:
entity_id: media_player.kitchen_speaker
message: Turn the lights off in the downstairs toilet
Let me breakdown the parts of the code:
Id: We need this to define a name within home-assistant, this is used in home assistant and is the entity reference, best practice is to use underscore (_) between words.
Alias: Is a friendly name this can be anything you wish, mainly for humans.
Trigger has 5 lines of code related to it, first the trigger platform is state, the entity_id will be the specific light bulb in your configuration, we are looking to trigger when the bulb is off and moves to on for about 5 seconds.
Now this is for demo purposes it is better to set a time that is appropriate within your household.
Action: in this part we are connecting to the google mini and making it talk!
In order to do so we are going to call the service tts.google_say (text to speech). In order to use this service you need to ensure that the following line of code is in your configuration.yaml:
# Text to speech
tts:
- platform: google_translate
service_name: google_say
Now you need to find your google mini within the configuration, mine is called media_player.kitchen_speaker.
The message is what google is going to read out load: “Turn the Lights off in the downstairs toilet”
How could we improve this?
This is really up to you to decide! I can give you a few examples:
Perhaps we can get the automation to turn off the light itself and let you know through google that someone left the light one but it has been taken care of…
This is a nice example of a thank you message whenever someone turns a light off, this will get annoying I guess if kept on 🙂
# thank you
- id: 'thank_you_loo_notification'
alias: Thank you Notification
trigger:
platform: state
entity_id: light.downstairs_loo
from: 'on'
to: 'off'
for: '00:00:01' # 5 seconds
action:
- service: tts.google_say
data:
entity_id: media_player.kitchen_speaker
message: Thank you for turning off the light, have a great day.
Please subscribe to the blog and my YouTube Channel!
Keep it digital!
DISCLAIMER: This blog post and description contain affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This allows us to continue to make videos like this.