Automations #2: the condition block

The function of the condition block in an automation is to prevent it from running.

Conditions affecting the flow of the automation (if-then, choose, etc) go in the action block; the condition block is for anything that will cause the automation to abort and do nothing…

You can have several conditions. If you do, they will be AND by default - they all need to be true before the automation can run. You can use NOT and OR if you wish (in the UI, click on the Add Building Block button), but if you find yourself needing to do this it’s worth considering whether two automations might be just as effective and easier to maintain.

You can use the condition block to add override switches that will “turn off” automations.

trigger:
  - platform: state
    entity_id:
      - binary_sensor.yard_motion_sensor
    to: "on"
condition:
  - condition: state
    entity_id: input_boolean.yard_override
    state: "off"
action:
  - service: light.turn_on
    target:
      entity_id: light.yard

In this example, if the override is on, the motion sensor will not turn on the light. The input_boolean could be a switch on a dashboard or it could be controlled by another automation - one triggered by light levels, for example.

Testing

You can test an automation in the UI by clicking Run in the three dots menu (top right).

However, this only runs the action block of the automation. To test conditions as well, go to Developer Tools | Services and use the service automation.trigger with the skip_conditions attribute set to to false.


The Home Assistant Cookbook - Index.

4 Likes