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.
Day Triggers when a day is scanned.
Template Triggers when a template contains the value true.
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.
Template Template Condition
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)}}

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:

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

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