Conditional action in an automation

I’m struggling to come up with a way to have one button toggle between different states of the light. I’m trying to get it to work in the following way:

  1. The light is off.
  2. First button press toggles the light on, sets the brightness to 50/256 and temperature to 2200 K
  3. Second button press increases the brightness to 150 and changes temperature to 2800 K
  4. Third button press increased the brightness all the way up to 255 and temperature to 3200 K
  5. Fourth button press turns the lift off, so moving back to step 1.

I don’t want to write 4 different automations for this, each with each own condition. I’d like to have one automation. I’m trying this approach (for now only two states):

alias: ...
trigger:
  - ... truncated for brevity ...
condition: []
action:
{% if is_state('light.lamp', 'off') %}
  - action: light.turn_on
    data:
      entity_id: light.lamp
      brightness: 50
{% else %}
  - action: light.turn_off
    data:
      entity_id: light.lamp
{% endif %}
mode: single

The syntax isn’t valid though. I’m struggling to come up with a way to have a conditional item in the actions array that would depend on the state. Tried to find a way to do that in Jinja docs, but to no avail. I’d appreciate any pointers. Thanks!

An automation action block is a script, which has all the logic control you need:

Jinja templating cannot be used just anywhere. Where it can be used, it is used to provide values in key: value pairs.

Depending on exactly how you want the process to work you have a couple options:

  1. Have your button increment a counter entity, then have the automation change the light based on the counter’s value.
  2. Use a series of Wait for Trigger actions.

If you clarify what your specific goal is, we may be able to give you more specific advise.

Thanks for that. Reading up now on scripts, I found a section titled Choose a group of actions that sounds exactly like what I need:

- choose:
    # if
    - conditions: ...
      sequence:
        - action: ...
    # elif
    - conditions:
        - condition: ...
      sequence:
        - action: ...
  # else
  default:
    - action: ...

I’ll try that approach. Thanks again!

edit: I’ll try to use that over the coming days. As I don’t have much time to play with this, it might take me a while, but I’ll circle back here and mark the answer as a solution if it’ll work.

I’ll try first the choose approach in a script suggested by @Troon, as it doesn’t, hopefully, require me to add a counter. A counter (which I haven’t used yet, so might be wrong) sounds like a new piece of state maintained just for my automation. To keep things robust and simple I’d prefer to rely on the existing state of that light. This way I won’t ever have to worry about the counter not being in sync with the state of the light. But should the choose approach not work, I’ll look at counters more closely, thanks for the tip.

Does your button have multi-press functionality?

Unsure, I’ll check. It’s an Ikea Styrbar - it has 4 different buttons. But I want to utilise only one to control that light, I need the other 3 buttons to do other things.

Anyway, the choose approach worked. I didn’t even need a separate script. Thank you both for help! Here’s the full automation in case someone else will be trying to figure something similar out:

alias: Lamp
description: ""
trigger:
  - device_id: bf205b3d7712832a8d2180099d55917b
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: turn_off
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ is_state('light.lamp', 'off') }}"
        sequence:
          - action: light.turn_on
            data:
              entity_id: light.lamp
              brightness: 1
              color_temp_kelvin: 2202
      - conditions:
          - condition: template
            value_template: "{{ state_attr('light.lamp', 'brightness') <= 1 }}"
        sequence:
          - action: light.turn_on
            data:
              entity_id: light.lamp
              brightness: 150
              color_temp_kelvin: 2600
      - conditions:
          - condition: template
            value_template: "{{ state_attr('light.lamp', 'brightness') <= 150 }}"
        sequence:
          - action: light.turn_on
            data:
              entity_id: light.lamp
              brightness: 255
              color_temp_kelvin: 3000
    default:
      - action: light.turn_off
        data:
          entity_id: light.lamp
mode: single