Use Timers to build Reliable Automations in Home Assistant

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the common automation patterns we like to use in our Smart Homes is to have a motion sensor turn on our lights but then we need to automate when those lights turn off and tying our lights directly to the state of a motion sensor can be problematic because a lot of motion sensors return to the off State before the attempt to find motion again which means your lights could cut out when you need them the most to solve that we typically use the 4 attribute in an automation trigger so that the motion sensor has to be in the off state for a set period of time before the automation to turn off our lights is triggered but if you want to build solid automations then you shouldn't be using the 4 attribute and in this video I'm going to tell you why and show you a better way of automating the boring stuff [Music] foreign s my name is Jeff I've been a big fan of that 4 attribute ever since it came out but having recently decided to rebuild most of my automations and doing a lot of it in the UI has exposed an issue that four attribute isn't persistent I have a wax warmer in my studio that anytime it gets turned on an automation turns it off after 20 minutes and to accomplish that I'm using the four attribute but if I decide to create or modify an Automation in the UI or I reload my automations manually or for some reason home assistant restarts because of power flickers or I decided that moment that it's time to catch up on core updates all of my automations stop running which means home assistant loses all awareness of those four attributes so in that case the wax warmer won't ever turn off until I manually turn it off but thankfully there's an easy fix for that and it's called a timer [Music] foreign ER is a type of Helper and it's exactly what it sounds like a clock that counts down a set period of time but because there's a lot of context around a timer's current state it uses the event bus to communicate that context and state with the rest of Home assistant which makes automating around them a little more complicated than your normal triggers but I'm going to walk you through it first we need to create a timer to create a timer we need to set up a helper inside of Home assistant and to do that we're going to head over to our settings menu and then we're going to choose devices and services then under here we'll click on helpers now we just need to click the create helper button and we're going to choose timer for this one I'm going to give it the name of Studio wax warmer then for the icon let's just choose we'll just use this air filter one and for duration I'm going to put 20 minutes now we're also going to want to click this restore box and this restore box is extremely important if you want this timer to be persistent over restarts and after that we're going to click create as soon as the timer starts home assistant sets the state to active and sets the finished at time stamp based on the timer duration and as long as you chose that restore option when you created the timer home assistant is going to remember that time even if it restarts this means when home assistant comes back up after a restart it will check to see if any timers have the active State and for any of them that do it'll evaluate whether or not that finished at timestamp occurred in the past and if it it it's going to Mark those timers as being finished at that point which will trigger any automations waiting for that timer to finish likewise if you pause a timer and then restart home assistant when home assistant comes back up that timer will still be paused at the point that it was as long as you use that restore option if you didn't use the restore option when home assistant comes back up all of those timers get set back to its default State when the timer does reach zero home assistant will fire a timer finished event sets the status to idle and clears the finished ad timestamp if you cancel the timer home assistant fires a timer cancel event and you can also restart a running timer by calling the timer restart service which simply fires an event that the timer restarted and sets the countdown back to the beginning and since those timers are firing all of those different events based on different contexts we can use those events inside of our automations foreign now that we have a timer set up and we've covered a little bit about what's happening inside those timers we can use those timers in our automations for this example we're going to modify that wax warmer automation to use a timer instead of that 4 attribute so in the current automation I have for the wax warmer in my studio space under the trigger I'm using that for attribute for 20 minutes so we're going to modify this so that we can take advantage of that timer we created earlier so to do that we're going to remove this for attribute because we're not going to use it anymore but we're also going to need to modify this automation to use the choose action so I'm going to add a trigger ID to this trigger and we'll call it wax on and we'll jump down to our actions here and we're going to go ahead and add another action here we'll choose the choose action and for that first condition we're going to use the triggered by we'll choose our wax on and all we have to do here now is to start our timer so for the call service we're going to use the timer dot start and then we have to choose the entity of our timer and we have our studio wax warmer timer we'll just pick that and for this condition we should be good now anytime that our wax warmer is turned on that timer is going to start and we can go ahead and delete this other one but now we need to handle when that timer finishes to turn off the wax warmer so we're going to add another trigger this time we're going to use the event because the only way to know when the timer is finished is to listen for that finished event so for event type we're going to put timer dot finished and here under event data we're going to put entity ID and we're going to choose that timer and we'll also give this one a trigger ID and we'll call it wax off now we need to jump back down to our actions and add another option this time for that trigger ID wax off and then under actions I'm going to call service but this time we're going to use the switch turn off service and we're going to choose that studio air freshener which is the wax warmer and now we've replaced that four attribute with our timer and we can hit save and we should be good now anytime the wax warmer turns on that timer should start and then at the end of that 20 minute timer home assistant will see the event has fired and will turn off the wax warmer which will make this automation much more reliable now so far we've talked about how to use a timer to replace a four attribute in an automation but the issue I brought up at the beginning of this video also affects delays wait for templates and even repeat Loops if you reload the automation system either manually through that yaml menu or because you've saved an Automation in the UI home assistant is going to stop all of the currently running automations which means those wait for templates delays and repeat Loops are just going to end and your automation is never going to finish but I think it's important to point out that while timers can help with some of that they're not a silver bullet and I think you can definitely go overboard with timers and automations but you should definitely take a look at your critical automations to see where timers could step in and make those automations a little more reliable anyway I hope that helps you on your mission to automate the boring stuff let me know in the comments what you plan on using timers for or if you're already using timers somewhere else in your home assistant setup and if you're looking for other ideas on how to leverage home assistant then check out this video that YouTube thinks would be perfect for you turns out I agree and if you haven't watched it you should definitely check it out anyway that's it for this video now it's time to go automate the boring stuff [Music] [Music]
Info
Channel: SlackerLabs
Views: 32,995
Rating: undefined out of 5
Keywords: home assistant automation examples, home assistant basics, home assistant for beginners, home assistant beginner's guide, smart home ideas, home assistant tutorials, smart home tech reviews, advance home assistant automations, build a smart home, home automation for beginners, home automation tutorials, home automation tips, home assistant ideas, home assistant timers, using timers in your automations, build better automations, dont use the for attribute
Id: SuLPMxQUv0Q
Channel Id: undefined
Length: 8min 33sec (513 seconds)
Published: Wed Oct 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.