If-then(-else) actions are finally coming to Home Assistant

Disclosure: This post contains affiliate links. If you click through and make a purchase, I will earn a commission, at no additional cost to you. Read my full disclosure here.

When I started experimenting with programming languages like BASIC and later C, the concept of if-then-else conditionals was a fundamental aspect that stuck with me. Over the last twenty years, this basic conditional statement has remained an essential tool in my programming repertoire. After migrating most of my automations from Node-RED to the Home Assistant Dashboard, I found myself missing this straightforward conditional logic. Although the Choose action in Home Assistant enables creating anything from basic to complex if-then-else structures, I always wished for a more streamlined and cleaner method. It seems I wasn’t alone in this desire. With the release of Home Assistant Core 2022.5, our requests have been answered. Home Assistant now supports a native if-then-else action. While this new feature doesn’t introduce any functionality that wasn’t already possible, it significantly simplifies and accelerates the process of creating automations.

Contents

Understanding the If-Then-Else Action in Home Assistant

The new if-then-else action in Home Assistant allows users to define automations with a straightforward conditional structure. Here’s a basic rundown of how it works:

  1. If Condition: This is the primary condition that needs to be met for the subsequent actions to be executed.
  2. Then Action: If the condition is met, the actions specified here will be executed.
  3. Else Action: If the condition is not met, the actions specified here (if any) will be executed.
The image shows a Home Assistant automation configuration screen for setting up an if-then action. The interface is divided into sections. The selected action type is “If-then.” In the “If” condition section, the condition type is set to “Device,” with the device being “office/sensor/motion/ceiling.” The specific condition is that the office/sensor/motion/ceiling occupancy is detecting motion, with a duration of 0 hours, 0 minutes, and 0 seconds. In the “Then” action section, the action type is also set to “Device,” with the device being “office/light/ceiling_lamp.” The specified action is to turn off the office/light/ceiling_lamp. There is also an option to add more conditions and a test button to verify the configuration.

Example Scenario

Let’s consider a simple example: turning on a light based on the time of day. Previously, this could be achieved using the Choose action, but the new if-then-else structure makes it more intuitive.

Before (Using Choose Action)

choose:
  - conditions:
      - condition: time
        after: '18:00:00'
    sequence:
      - service: light.turn_on
        entity_id: light.living_room
  default:
    - service: light.turn_off
      entity_id: light.living_room

Now (Using If-Then-Else Action)

if:
  - condition: time
    after: '18:00:00'
then:
  - service: light.turn_on
    entity_id: light.living_room
else:
  - service: light.turn_off
    entity_id: light.living_room

As you can see, the if-then-else action reduces complexity and makes the automation script more readable.

Benefits of Using If-Then-Else Actions

One of the primary benefits of the if-then-else action is the simplification it brings to creating automations. The syntax is more straightforward, reducing the learning curve for new users and speeding up the process for experienced ones.

By using if-then-else actions, the logic of your automations becomes more apparent. This enhanced readability is beneficial not only for your understanding, but also for anyone else who might interact with your Home Assistant configurations.

Simpler and more readable automations are easier to maintain. When you need to make changes or troubleshoot issues, the if-then-else structure allows you to quickly identify the logic and understand how different conditions and actions are linked.

Conclusion

The introduction of if-then-else actions in Home Assistant Core 2022.5 is a significant improvement, making the creation of automations more intuitive and efficient. This feature simplifies the process, enhances readability, and improves maintainability, making it a valuable addition for both novice and experienced Home Assistant users. Now, creating complex automation flows is easier and more accessible, empowering you to better manage and automate your smart home environment.

A portrait photo oif Liam Alexander Colman, the author, creator, and owner of Home Assistant Guide wearing a suit.

About Liam Alexander Colman

is an experienced Home Assistant user who has been utilizing the platform for a variety of projects over an extended period. His journey began with a Raspberry Pi, which quickly grew to three Raspberry Pis and eventually a full-fledged server. Liam's current operating system of choice is Unraid, with Home Assistant comfortably running in a Docker container.
With a deep understanding of the intricacies of Home Assistant, Liam has an impressive setup, consisting of various Zigbee devices, and seamless integrations with existing products such as his Android TV box. For those interested in learning more about Liam's experience with Home Assistant, he shares his insights on how he first started using the platform and his subsequent journey.

Leave a comment

Share to...