Automating Every Minute of My Day
There are around 50 automations shown in my recent video! Some are advanced and others are very simple. I’m going to focus on the more challenging ones in this article. That way it can help you solve those difficult automations.
The automations will be the same order as in the video, except for the more simple ones that I will skip.
I’m using Home Assistant for all my automations. If you are just getting started with Home Assistant, then check out the getting started with Home Assistant video I made.
Phone Alarm Integration
Having my wake up automation dynamically change to what my phone alarm is set to has been great. I only need to worry about setting my phone alarm and nothing else. Setting it up was slightly challenging.
This might be possible on iOS, but with Android it’s built in to the Home Assistant companion app. You will need to enable the “Next alarm” in the Sensors section. To find that, open up the Home Assistant app on your phone. Select the Settings, choose “Companion app,” and then select “Manage sensors.” Search for “alarm” and make sure it’s enabled.
There are multiple ways to make this work, but to make things more simple I created a Template Sensor in the Helpers section. Here is my template code.
{{ (((as_timestamp(now()) | int) + 2*60) | timestamp_custom('%Y-%m-%d %H:%M:00')) > (((state_attr('sensor.your_phone_next_alarm', 'Time in Milliseconds') | int / 1000) ) | timestamp_custom('%Y-%m-%d %H:%M:00')) }}
What this will do is make this Template Sensor “True” 2 minutes before the alarm goes off. If you want to change how many minutes, then change the 2 in “2*60” so 5*60 would be 5 minutes before the alarm goes off.
Then make sure to change “sensor.your_phone_next_alarm” with the entity of your phone’s alarm entity.
For the automation, it’s very easy to trigger now that the Template Sensor can be used. Here is the YAML for the first wake up automation trigger.
entity_id:
- sensor.reeds_phone_alarm_active
to: "True"
trigger: state
I would recommend having some conditions for this. Like a check if you are home, so this automation isn’t running if you are on a trip and your spouse is trying to sleep in.
The second automation has the same trigger, but has a wait for 5 minute action and then a Condition in the action to check if I am still in bed.
actions:
- delay:
hours: 0
minutes: 5
seconds: 0
milliseconds: 0
- condition: state
entity_id: binary_sensor.withings_in_bed_reed
state: "on"
I’m using Music Assistant to play the song in the morning.
Bluetooth headphones
I’ve shown how to run an automation when connecting a certain Bluetooth device in the past with iOS using a Siri Shortcut. With Android, it can all be done in Home Assistant. The “Bluetooth connection” has to be enabled in the Companion app sensors.
Similar to the automation above, I created a Template Sensor. This time I choose a Binary Template Sensor to know if the connection to my running Bluetooth headphones is active or not.
{% set c = state_attr('sensor.your_phone_bluetooth_connection',
'connected_paired_devices') %}
{{ c is not none and c is search('Shokz') }}
If you use this code, make sure to replace “your_phone_bluetooth_connection” with the entity for your phone’s Bluetooth connection and change the name of your Bluetooth headphone name from “Shokz” to whatever your Bluetooth name is called.
If you don’t know what it’s called just look it up in the Developer Tools section under States.
The automation trigger is easy now since I just need to know if the Binary Template Sensor changes to True.
trigger: state
entity_id:
- binary_sensor.phone_connected_to_shokz
to: "on"
Speaker reading off calendar and to-do list for the day
This was probably the most challenging to figure out but I wanted it so bad that I finally got it working. Having my smart speaker read off the calendar events and my Todoist items for today.
Here’s how to do it in an automation. First, make a trigger be something like walking in a room. Mine is when I turn on my shower light in the bathroom. I have a condition that I have to be in the room and that it’s in the morning.
I have 3 actions. One is a “calendar.get_events” call that gets my Google calendar events, and my Todoist list for today. Since Home Assistant treats Todoist as a calendar it makes it easy. Then the second action reads out loud the calendar events and the third action reads off the Todoist items. I had to separate them out for it to work.
Here is the first action. Make sure to replace “your_google_calendar” and “your_todoist” with your own entities.
action: calendar.get_events
target:
entity_id:
- calendar.your_google_calendar
- calendar.your_todoist
data:
duration:
hours: 12
minutes: 0
seconds: 0
response_variable: agenda
enabled: true
Then to read it off on the smart speaker there are multiple ways but I’m using Piper. Also, for some reason the speaker reads off the actions in reverse order.
action: tts.speak
metadata: {}
data:
cache: false
message: |-
To do list items for Today:
{% for event in agenda["calendar.your_todoist"]["events"] %}
{{ event.summary }},
{% endfor %}
media_player_entity_id: media_player.unnamed_room
target:
entity_id: tts.piper
enabled: true
action: tts.speak
metadata: {}
data:
cache: false
message: |-
Calendar Events for Today:
{% for event in agenda["calendar.your_google_calendar"]["events"] %}
{{ event.summary }}: at {{ as_timestamp(event.start) | timestamp_custom('%-I:%M %p') }},
{% endfor %}
media_player_entity_id: media_player.unnamed_room
target:
entity_id: tts.piper
enabled: true
Computer turning on also turns fan on
One of the great things about Home Assistant is the companion app on the computer. I don’t use Windows, but on a Mac the Home Assistant Companion app works very similar to the phone app.
To trigger the automation, I’m using the “Active” sensor. It’s the top one if you are viewing the device.
trigger: state
entity_id:
- binary_sensor.macbook_pro_active
to: "on"
I don’t want this automation running when I’m using my computer away from my desk, so I have another condition in the actions to check if another display is connected after 5 seconds. I added the 5 second delay because sometimes it takes a second for the computer to register it with Home Assistant.
actions:
- delay:
hours: 0
minutes: 0
seconds: 5
milliseconds: 0
- condition: numeric_state
entity_id: sensor.macbook_pro_displays
above: 1
enabled: true
Then I turn on my fan or whatever else I want to do.
Video call automation
Just like the last automation, this one uses the Home Assistant Companion app on the computer. It has access to know if the camera is being used or not.
Here is the trigger to the automation when the camera turns on.
trigger: state
entity_id:
- binary_sensor.macbook_pro_camera_in_use
to: "on"
Make sure there is a condition so this only runs during “business hours” or when you would normally work. That way the automation isn’t firing off when you are having a video call with family at night.
To make everything go back to normal, the trigger for the automation is almost the same, but instead of on, it’s off.
trigger: state
entity_id:
- binary_sensor.macbook_pro_camera_in_use
to: "off"
Rebooting devices that go offline
This is one of my favorite automations because it runs in the background and keeps my smart home running smoothly. When a device goes offline, like my light strips, it physically reboots them.
There are more things that can be done like reloading the integration and I can talk about that another time.
But here is the automation trigger YAML. Basically if the entity (or light strips in my case) goes offline for 5 minutes, the automation will be triggered.
entity_id:
- light.wled_studio_corner
to: unavailable
for:
hours: 0
minutes: 5
seconds: 0
trigger: state
Then for the actions it uses the smart outlet to turn the light strips off and then back on. It then checks in the Actions if the other lights in my studio are on or off. If they are all off, it will turn the light strips off too.
Package reminder on dashboard
Sometimes when a package or something is put on our front porch, we don’t grab it right away. This automation helps us to remember to check the porch for a package.
The automation is triggered when the doorbell detects a person. The Unifi G4 can detect packages and that is available in Home Assistant, but I’m just using the person detection since it could be a neighbor dropping something off and not a “package”.
Then if the front door isn’t opened in a few minutes, an input boolean is turned on and that is what shows up on the dashboard. When the button on the dashboard is clicked, the input boolean is turned off and the doorbell live feed shows for 30 seconds.
Here is the automation that turns on the input boolean when someone is spotted on the doorbell. It waits 2 minutes and checks to see if the front door has been closed for at least 3 minutes. Essentially, it front door has been closed longer than the person has been spotted for.
Here is what the “Test IF Front Door is Closed for 3:00” looks like.
Pick up kid from school reminder
This is kind of a simple one, but I’m going to include it anyways. When it’s a certain time and I haven’t left to go pick up my kids from school, then I get a reminder.
The trigger is just a time based trigger. It’s the condition that is the important part and that checks to make sure the garage doors have both been closed for the last 10 minutes. So if we open up the garage door to go get them, then this wouldn’t run.
Here is the YAML for the condition for the garage doors. Make sure it’s an “and” condition if you have two garage doors. That way they both have to have been closed for 10 minutes.
condition: and
conditions:
- condition: state
entity_id: cover.garage_door_1
state: closed
for:
hours: 0
minutes: 10
seconds: 0
- condition: state
entity_id: cover.garage_door_2
state: closed
for:
hours: 0
minutes: 10
seconds: 0
Calendar event before next phone alarm
Having a really early call doesn’t happen too often. But when it does, I usually sleep right through it because I forget to wake up early. So what’s the solution… make an automation to fix it!
For this one, I made another Binary Template Sensor. But you could easily just put this as a template condition in the automation. What it does is check to make sure an alarm is set and my calendar is not empty. If so, it checks to see if the calendar event is before the phone alarm.
Just make sure to replace “your_phone_next_alarm” and “your_calendar” with your own entities.
{% if states.sensor.your_phone_next_alarm is not none and (as_timestamp(state_attr('calendar.your_calendar', 'start_time'))) is not none %}
{% if (as_timestamp(state_attr('calendar.your_calendar', 'start_time'))) < (state_attr('sensor.your_phone_next_alarm', 'Time in Milliseconds') | int / 1000) %}
true
{% else %}
false
{% endif %}
{% else %}
false
{% endif %}
I’m using a “Choose” action in the automation. If the Binary Template Sensor (next_calendar_before_phone_alarm) is true, read off one message and if it’s false, read off another.
choose:
- conditions:
- condition: state
entity_id: binary_sensor.next_calendar_before_phone_alarm
state: "on"
sequence:
- alias: Announce on bedroom speaker
data:
entity_id: media_player.nesthub
message: >-
Everything turned off. But heads up, next calendar event is before
your phone alarm
action: tts.google_translate_say
default:
- alias: Announce on bedroom speaker
data:
entity_id: media_player.nesthub
message: "Everything turned off in the house. "
action: tts.google_translate_say