automations, scripts: bring your home to life

I wanted to extend my hass.io with automations and scripts but as usual when I attempted to break into a new field of hass.io I failed to find a quick walkthrough online of how to get started. These are my learnings.

Automations are very handy when you want a trigger (the time becomes 10:00 a.m, the sun sets or a motion sensor is enabled) to create an action. I find scripts more suitable when you want a number of actions to be executed in series. Quite often I end up with an automation or a user interface button starting a script.

There are some basic things that needs to be in place in hass.io for automations and scripts to work.

Create an automations.yaml and a scripts.yaml in your config folder. These are the files where you write the actual code. For hass.io to know that the code are in these specific files you need to point to them in the configuration.yaml.

In configuration.yaml add
automation: !include automations.yaml
script: !include scripts.yaml
In automations.yaml put the automation code; this examples turn of the living room lamp at 08:30.
- id: '1570820783744'
  alias: light shutdown at 8:30
  trigger:
  - at: '8:30'
    platform: time
  action:
  - data:
      entity_id: light.living_room
    service: light.turn_off
Then it took me some time to figure out that each time I did a change to automations.yaml I needed to re-read the file into hass.io. This is done through the web interface: hass.io > Server Controls > Reload Automations.
Figure 1. For changes in automations.yaml to take effect you need to hit reload automations under server control.

If you can not locate Server Controls that is because you first need to enable Advanced Mode of hass.io. That is done by clicking your name below Notifications and then flip the switch to enable Advanced Mode.
Figure 2. Enable Advanced Mode in hass.io to be able to update automations and scripts.
Needless to say this took me some time to figure out, I thought my yaml code was faulty but instead there were other things preventing my automations and scripts from running as I intended.

The same  procedure needs to be followed to create and update a script. As en example a script that turn off six lights on the first floor in my house.

I put the code in scripts.yaml
lights_off_1st:
  alias: Lights Off First Floor
  sequence:
  - service: light.turn_off
    entity_id: light.lounge
  - service: light.turn_off
    entity_id: light.lounge_window
  - service: light.turn_off
    entity_id: light.kitchen_wall
  - service: light.turn_off
    entity_id: light.kitchen_ceiling
  - service: light.turn_off
    entity_id: light.piano_window
  - service: light.turn_off
    entity_id: light.hallway_wall
and then I call the script from a custom button card so that I can trigger the script from the user interface.
- type: custom:button-card
  tap_action:
    action: call-service
    service: script.turn_on
    service_data:
      entity_id: script.lights_off_1st
  entity_id: script.lights_off_1st

In the same way, to get this script to take effect you need to hit Reload Scripts under Server Control.

Please let me know in the comments field if you have any questions or comments.

Comments