Build Smarter Automations in Home Assistant Using Scripts

Build Smarter Automations in Home Assistant Using Scripts

Automations may be the one thing that brings all of smart home enthusiasts to the yard. After all, its is the in the name Home Automation.

However, the problem is unless you are willing to invest time writing code, the automation you can build out of the box with most platforms is a slightly more advance version on If This Than That. Which is find if all you want to do is automate your house. But if you are like me and you want to build a smart home – we are talking JARVIS smart – you are going to need your automation platform to be able to make decisions based on context.

A while back I decided to se if I could push Home Assistant into the realm of a true smart home. To me, a smart home is one that anticipates our needs and reacts to them instead of waiting for us to trigger an action via a command. To help guide me I wrote three guiding principles I called The Three Laws of home Automation. And the goal was to eliminate as much physical interaction as I could. Part of that required building smarter automations.

Thankfully Home Assistant allows automations to call scripts. Scripts are essentially routines you could find in Alexas or Google Home. They are just a sequence of events or actions, but in Home Assistant that can have their own conditions.

For example you could have a script that fires when the family arrives home.

  - id: family_has_arrived
    alias: Family Has arrived
    - platform: zone
      entity_id: person.jeffrey
      zone: zone.home
      event: enter
   - platform: zone
      entity_id: person.katherine
      zone: zone.home
      event: enter
    mode: single
    - service: script.family_is_home
    - service: script.driveway_on
    - service: script.standby
    initial_state: true

In the action of this automation you could call three scripts, and each of these scripts could have their own conditions.

In the above example, the script driveway_on fires every time someone arrives home, but has a condition that checks to see that the sun is below the horizon and if it is turns one the driveway lights.

The standby script will disarm the security if it is armed.

And the family is home script will check to see if the family is marked away and then change the sensor to home.

In this example we are moving any conditions to the scripts so that we can have one a general trigger, and let the scripts determine what actions are taken. This reduces the number of automations you need.

In a future video I will discuss using the Choose action to something similar. If you would like to know more of how I am using scripts to make Home Assistant Automations smarter check out my YouTube video.

Until Next time, Automate the Boring Stuff.