Home Assistant Automation - Possibilities & Basics

From controlling the pool pump to the heating to the robotic mower, automations in Home Assistant offer a way to make things around your home really smart.

Initially, I only used the automations in Home Assistant to replace a timer for my pool pump. To do this, I first created an automation for turning it on and one for turning it off: Works, but somehow two automations for controlling one device still don't feel quite right. For this reason, but also because more sophisticated processes have arisen in the meantime, I looked at the possibilities of the automations in detail. Most recently, I was even able to replace my previous robotic mower control with a Home Assistant automation, see: Controlling my robotic mower.

Create automation

A new automation can be added in the settings and "Automations & Scenes".

 

The concept: Trigger -> Condition -> Action

Automations are started by one or more stored triggers, as an example at a certain time, or when the status of a sensor or device changes. Before executing one or more actions, the automation can still be checked for certain conditions.

Here is a short overview of the possible triggers for starting an automation:

Trigger

Trigger Description
Event The heart of the Home Assistant is the event bus. All status changes in Home Assistant trigger corresponding events, which can be used as triggers for starting an automation.
Geolocation The geolocation trigger is fired when an entity appears in or disappears from a zone. As an example, when a weather event occurs in the zone Home Assistant is in.
Device Triggers when the status of a specific device changes, for example, when a device is turned off or on, or when a specific value, such as the temperature of a temperature sensor, is above or below a specified value.
Home Assistant Triggers when Home Assistant starts or stops.
Calendar Calendar entities can be used and provided by other integrations. Calendar triggers allow automation based on the start or end of a calendar entity event.
MQTT Triggers when an MQTT message is received.
Numeric state Triggers when an entity's numeric value is above or below a specified value.
Sun

Triggers before sunrise or sunset, or after sunrise or sunset.

Optional: Offset: Format: HH:MM:SS
Example: Offset: -02:00:00 = 2 hours before sunrise or sunset;
Example: Offset: 02:00:00 = 2 hours after sunrise or sunset.

Day Triggers when a day is scanned.
Template

Triggers when a template contains the value true. E.g. when one entity becomes larger than another:
{{ states("sensor.entity1") | float > states("sensor.entity2") | float }}

see also: Home Assistant Templates / Statistics and Sensors

Webhook

Triggers when a webhook payload is received on the /api/webhook/{ID} endpoint. The ID is generated when the trigger is created:

Webhooks support HTTP POST, PUT, and HEAD requests. When calling the webhook endpoint, form data, JSON data or URL query parameters can be passed to the template: trigger.data, trigger.json or trigger.query.

Time Time triggers, at a fixed time or via a date/time helper.
Time pattern Time pattern trigger when the hour, minute or second of the current time matches a specified value.
Zone Triggers when an entity with location enters or leaves a zone. e.g. device tracker entities. As an example when a WLAN device is Home or Away, see: ha-integrations#device-tracker-openwrt.
State Triggers when the state of an entity changes.

Condition

Optionally checks the started automation for certain conditions.

Condition Description
Triggered by Checks if the automation was started by a specific trigger (specific trigger ID).
Device Checks if a specific device has a specific status or value.
Not Not can be used to check if one of the conditions listed here is not met.
Numeric state Checks the numeric value of an entity to see if it is above or below a specified value.
Or Link multiple conditions if one of the conditions is met.
Sun

Before sunrise or sunset, or after sunrise or sunset.

Optional: Before offset or After offset: Format: HH:MM:SS
Example: Before offset: 02:00:00 = 2 hours before sunrise or sunset

Template Template Condition, see also: Home Assistant Templates / Statistics and Sensors
And Link multiple conditions when multiple conditions are met.
Time Checks for a fixed time or via a date/time helper.
Zone Checks if an entity with location is in a zone or not....
State Checks the state of an entity

If the stored conditions apply, the defined actions are executed:

Action

Action Description
Select Execute one or more actions based on one or more conditions.
Condition Verify that a unit is in a particular condition.
Execute service Perform a specific service, for example, send a notification to a cell phone with Home Assistant app installed.
Event Trigger a specific event
Device Perform an action on a specific device, for example, turn a specific device on or off.
Play media Play media on a media player
Run in parallel Start multiple actions at the same time
Stop Stop automation
Scene Activate a specific scene
Wait for time to elapse (delay) Waiting time for the automation. So that the following actions are started with a delay.
Wait for a template Waiting for a template to be fulfilled.
Wait for a trigger Wait for a trigger, including timeout.
If-then If, then, else (If-then, else). If certain conditions are met start a certain action, otherwise execute a certain other action.
Repeat repeat an action several times

YAML

When creating an automation in the visual editor, a YAML text with all relevant information is created in the background. The YAML text can be edited and customized in a text editor.

Processes

In addition to the history, the processes of already executed automations can be analyzed in detail:

Sensor unknown

If a numerical comparison: greater than, or less than - is used in the automation, the automation will stop if the sensor has the status "Unknown" for some reason:

if/condition/3/entity_id/0
Executed: May 4, 2023 at 7:20:27 AM
Result:
result: false
message: value 'unknown' is non-numeric and treated as False

This can be remedied by using a "Value template" in the automation:

{{states('sensor.rain_zamg_12') | round(2,default=0)}}

Switching on and off: one automation or two?

As an alternative to two automations for switching a device on and off, an ID can also be assigned in the triggers and used in the actions:

By means of the ID, the actions can be reacted to in an if-query and the device can be switched on or off accordingly:

Practice

mowing robot: Rain and sun control via a power outlet.

As mentioned in a previous post, I control my robotic lawnmower, a Husqvarna Automower, affectionately known as Oswald, via a controllable power outlet. The principle behind this is that the mower only starts when the charging station gets a current from the socket. My motivations for the power outlet were the lack of a rain sensor and the lack of available control options. For a few years I used a simple PHP page for control, see: www.script-example.com/smartplug-steuerung and later on I developed it with the PHP framework Laravel for the control. After transferring more and more automation tasks to Home Assistant in the meantime, the robotic mower was another logical step. Compared to the earlier solutions, the control with Home Assistant was much faster and easier to implement, especially since I don't have to worry about collecting the data, evaluating and displaying it. I was able to take over the logic for the control from the basic idea of the existing projects. I have limited the mowing phase directly in the settings of the device to the morning, so that Oswald disturbs as little as possible. Home Assistant is supposed to turn on the outlet only when it hasn't rained, the weather forecast is right, and the sun is shining, so that the device doesn't come into conflict with certain nocturnal or crepuscular animals. 

The automation in detail

Besides certain times, the power generation of my balcony power plant serves as a trigger. The mower starts in the morning when the sun is shining and is deactivated again in the evening when the mower is in the charging station. I can tell if the mower has found its way back to the charging station by the power consumption: Under 3 watts, the mower is fully charged and in the station. If the mower ever fails to find its way home, I get a notification on my cell phone. As a condition for the start, I have queried the weather in the automation: The past precipitation values and a weather forecast are used:

Trigger Description
When BalkonKW Total AC Output Power (Active) is above 110. Above 110 watts from the balcony power plant: the sun is shining:
In the actions: Check if the mower should start and turn on the power outlet if necessary.
If the time is equal to 18:00 In the evening
In the actions: Check if the mower is in the charging station and if necessary turn off the socket, or send a notification.

Actions:

[+]

Execute 'Switch on?' if 'Morning' otherwise 'Evening: Turn off'

If*:
Morning (before 1PM)
Then*:
Turn on?

If*:
Confirm BalkonKW Total AC Output Power (Active) is above 110 (sun shining)
Oswald Switch is off
Confirm rain_zamg is below 1 (no rain)
Weather Forecast (no rain in sight)

Confirm OpenWeatherMap Forecast Precipitation is below 50
Confirm OpenWeatherMap Forecast Precipitation is below 2

Then*:
OswaldSwitch on
Calling a service 'notify.mobile_app_sm_??' on

Otherwise:
In the evening: Switch off

If*:
Oswald Switch is on
Then*:
Execute 'Turn off Oswald Switch' if 'Confirm Oswald Active power is below 3' Otherwise 'Invoke a service 'notify.mobile_app_sm_??' on '

If*:
Confirm Oswald Active power is below 3
Then*:
Switch off Oswald Switch
Otherwise:
Call a service 'notify.mobile_app_sm_??' on

 

 

 Here is the finished process as YAML-Text:

[+]
alias: Oswald
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.balkonkw_total_ac_output_power_active
    above: 110
  - platform: time
    at: "18:00:00"
condition: []
action:
  - if:
      - condition: time
        before: "13:00:00"
        alias: Late morning
    then:
      - if:
          - condition: numeric_state
            entity_id: sensor.balkonkw_total_ac_output_power_active
            above: 110
          - condition: device
            type: is_off
            device_id: ???
            entity_id: switch.oswald_switch
            domain: switch
          - condition: numeric_state
            entity_id: sensor.rain_zamg
            below: 1
          - condition: and
            conditions:
              - condition: numeric_state
                entity_id: sensor.openweathermap_forecast_precipitation
                below: 50
              - condition: numeric_state
                entity_id: sensor.openweathermap_forecast_precipitation
                below: 2
            alias: weather forecast
        then:
          - type: turn_on
            device_id: ???
            entity_id: switch.oswald_switch
            domain: switch
          - service: notify.mobile_app_sm_??
            data:
              title: It's dry and PV supplies -> Oswald on
              message: "{{states('sensor.balkonkw_total_ac_output_power_active')}}Watt"
              alias: PV supplies? Turn on?
        alias: Turn on?
    else:
      - if:
          - condition: device
            type: is_on
            device_id: ???
            entity_id: switch.oswald_switch
            domain: switch
        then:
          - if:
              - condition: numeric_state
                entity_id: sensor.oswald_active_power
                below: 3
            then:
              - type: turn_off
                device_id: ???
                entity_id: switch.oswald_switch
                domain: switch
            else:
              - service: notify.mobile_app_sm_??
                data:
                  title: Oswald not turned off
                  message: "Power: {{states('sensor.oswald_active_power')}}Watt"
        alias: "In the evening: switch off"
mode: single

The Yaml-Text can be posted into an empty automation and adapted to your needs. The historical precipitation values, in the flow "sensor.rain_zamg" I get by curl from a publicly accessible website, similar to the readout of the PV values of my balcony power plant: PV Balcony Power Plant Commissioning and HA Integration. See also, HA: Use values of any WebGUI as entities

Balcony power plant or PV system surplus

Not always, but every now and then - my balcony power plant delivers a surplus of electricity on sunny days. So that I don't give it away, I start the pool pump when there is a surplus and stop it again when there is no more surplus power available.

Trigger Description
When meter_power is below -200 for 20:00

meter_power reflects the data of the smart meter: negative values (-) mean a power surplus

ID: surplus

When meter_power is above 0 meter_power = 0: no surplus

Actions:

Execute 'Switch PoolPump Switch on' if 'When triggered by surplus' otherwise 'Switch PoolPump Switch off'.

If*:

When triggered by surplus

Then*:

Turn on PoolPump Switch

Otherwise:

Turn off PoolPump Switch

I use the smart meter's excess power entity (meter_power) as the trigger for turning on: once for turning on, once for turning off. When the meter_power entity provides a power surplus of more than 200 watts for 20 minutes (-200watts), the trigger fires and starts the pump in the actions. The 200 watts is approximately the power consumption of the pump. To allow the automation to react in the actions between no surplus and a surplus, the When query decides whether the pump should be switched on or off. The trigger "When meter_power is below -200 for 20:00" has the ID "Surplus" for this purpose, this can be used as a condition for the if-query. Thus the threshold value for switching on (-200Watt) must be defined only once in the trigger and not additionally also in the if-query.

Automation in detail

[+]
alias: Sureplus PoolPump
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.meter_power
    for:
      hours: 0
      minutes: 20
      seconds: 0
    below: -200
    id: Sureplus
  - platform: numeric_state
    entity_id: sensor.meter_power
    for:
      hours: 0
      minutes: 0
      seconds: 0
    above: 0
condition: []
action:
  - if:
      - condition: trigger
        id:
          - Sureplus 
    then:
      - type: turn_on
        device_id: ???
        entity_id: switch.poolpump_switch
        domain: switch
    else:
      - type: turn_off
        device_id: ???
        entity_id: switch.poolpump_switch
        domain: switch
mode: single

Control heating - prepare hot water for the following day.

Besides the Control my heat pump I can alternatively heat the hot water via a switchable socket and a heating rod. Since my heat pump can only heat the water up to 50°C,the heating rod allows me to generate higher temperatures. If the hot water is heated by an additional 10°C on a day with sufficient sunshine, no hot water heating is required the following day. If the forecast for the expected PV harvest day for the following day falls below a certain value, I have put together an automation to heat the hot water more than usual with the PV electricity generated. Here is the automation in practice:

Legend: Green: PV power generation, Red: consumption; Yellow: heat pump; Dark red: heating rod

Automation in detail:

Trigger Description
When meter_power is below -3000 for 6:00

meter_power reflects the data of the smart meter: negative values (-) mean a power surplus

ID: surplus

When heating.hot water is above 60 If the hot water is above 60°C, the corresponding action should turn off:
When meter_power is above 0 no more PV power surplus

Actions:

[+]
Actions:

'Turn on hot water switch' execute if 'Confirm meter_power is below -3000, Confirm heating.hot water is below 60, and Confirm Pv production tomorrow is below 20' otherwise 'Turn off hot water switch'.

If*:

When triggered by surplus.

Confirm heating.hot is above 48 and below 60

Confirm Pv production tomorrow is below 20

Then*:

Turn on hot water switch

Otherwise:

Switch off hot water switch

 

 

YAML-Datei:
[+]
alias: "Surplus: hot water heater"
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.meter_power
    for:
      hours: 0
      minutes: 6
      seconds: 0
    below: -3000
    id: Überschuss
  - platform: numeric_state
    entity_id: sensor.heating_warmwasser
    above: 60
  - platform: numeric_state
    entity_id: sensor.meter_power
    above: 0
condition: []
action:
  - if:
      - condition: trigger
        id:
          - Überschuss
      - condition: numeric_state
        entity_id: sensor.heating_warmwasser
        below: 60
        above: 48
      - condition: numeric_state
        entity_id: sensor.pv_production_tomorrow
        below: 20
    then:
      - type: turn_on
        device_id: ???
        entity_id: switch.warmwasser_switch
        domain: switch
    else:
      - type: turn_off
        device_id: ???
        entity_id: switch.warmwasser_switch
        domain: switch
mode: single

 

📢 A new post will probably be linked here on 2023-10-02: Control heating with Home Assistant allow push notifications?

Conclusion

A Smart home solution unfolds its full potential only through the possibility to control certain devices automatically. The intelligent control of individual devices can not only increase comfort, but also save electricity or use it more wisely. And if certain events occur, an alarm can be sent to the cell phone.

positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Questions / Comments


By continuing to browse the site, you agree to our use of cookies. More Details