Two quick tricks for better HomeAssistant automations
It’s hard to believe, but the venerable Home Assistant project has just celebrated it’s 7th birthday with release .115! While HA does get better with each new release, it is by no means perfect :D. This post came about because of a new feature in release .115 and the subsequent limitations of that feature!
In the course of trying to integrate Home Assistant with all the things, everybody eventually hits some major limitation or bug and is forced to find some sort of workaround. Below are a few techniques that I’ve collected over the years to simplify some of my Home Assistant configuration and work around some of the platforms various limitations.
To be sure, nothing below is new or groundbreaking. I am just presenting how I use them in the hopes that they end up being useful to anybody else that googles similar problems.
(slightly) easier to maintain templates
Sometimes, you’ll have many entities in a given template and - depending on the complexity of the template - you may need to refer to each entity multiple times. This is a simple technique that allows you define the list of entities one time and refer to them as needed throughout the template in a succinct way. This makes the template a bit cleaner / shorter which inturn makes it easier to maintain.
To illustrate the technique, I’ve reproduced a template sensor that averages all of the different particulate matter readings from my outdoor weather station into a single number. Bonus: this approach works around the ‘only one entity’ limit with the statistics platform!
I want to get an average of 6 different entities:
- sensor.ws3_pm_0_3mm
- sensor.ws3_pm_0_5mm
- sensor.ws3_pm_10_0mm
- sensor.ws3_pm_1_0mm
- sensor.ws3_pm_2_5mm
- sensor.ws3_pm_5_0mm
Since each entity is nearly the same, it’s far easier to only store the small portion of the entity ID that is unique and programmatically generate the full entity ID once throughout the template.
| |
Use input_* in the trigger for an automation
The ’new’ feature in .115 that I eluded to is support for using various input_* objects in the condition section of an automation. This makes it possible to modify the parameters of an automation at run time! This was possible in the past, but would require a Rube Goldberg-esque setup chaining multiple templates/inputs/automations together; it was a mess to build and maintain!
Unfortunately, there is no support for input_* helpers in the trigger section of an automation. As of release .115, it is impossible to do something like this:
| |
If we can find an alternate way to trigger that automation, then we can move the comparison w/ the dynamic value to the condition block which brings our pipe-dream closer to reality :D. Fortunately, a significant number of automations do not require real time triggering so if we can ‘settle’ for an automation that may be delayed by a brief interval then we can use the cron-like time_pattern trigger to launch the part of the automation that we care about. In short, changing an automation from a push based trigger to a poll based trigger.
Here is an example automation that turns a bathroom fan on if the humidity in the bathroom rises above a user-configured threshold. The humidity sensor is battery powered and reports the humidity only on changes or after several seconds (whichever is longer). This means that the automation is already not going to trigger in ‘real time’ so what’s the harm in delaying it’s activation by a few more seconds?
| |
Yes, I absolutely could’ve used the state trigger to fire off the contrition check every time that a new humidity reading came in. I chose to use the ‘cron’ approach to illustrate how to make this technique work for two reasons:
- I find the
statetrigger to be useful when combined with theabove:/below:andfor:‘modifiers’. Currently, those modifiers are only supported in thetriggerblock and therefore can’t be configured withinput_*. The whole point is to have some aspects of the automation that can be adjusted at run time w/o having to edit the automation. - Some automations may not have a reliable/consistent push-based trigger event that they can use. In these cases, a regular polling approach is the only ‘fall back’.