How do you correctly do an IF THEN ELSE in Home Assistant Automation?

Here is the example a trigger event happens like a push of a Zigbee Switch… If the button is pressed and a given light is already “on” then I want one sequence of events to take place. If the same button is pressed and if the same light is already “off” then I want a different sequence of events to take place.

Today I am doing this by doing two automatons… both start with the same button press but one has an if light on and the other automation has if the light was off. As a programmer this bugs me because it should be IF a light is on do this ELSE do that. And all should be on ONE automation.

My problem is I didn’t see the way to program this in the system… I am SURE it must exists I just don’t understand how. So can someone please show me the way…

Thanks in advance.

Joe

if you’re new and learning, i think the best way is this:

then you can also drop to yaml mode and see how it’s done in the yaml…

if:
  - condition: state
    entity_id: light.ccc_1st_floor_steps
    state: "on"
then: []
else: []

Thanks that makes sense! It is just not where i expected to find it.

I was looking for it under the if section of automation but wasn’t thinking it is under the then do section.

I knew it had to exist. So thanks again!

If you have more that one if, take a look at the choose strategy. You can stack and nest these as needed.