Why is this if statement being triggered by a single button press?

The automation below is for a Sonoff smart button which controls lights in my kitchen. There is a “Button pressed” trigger, and also a “Button double clicked” trigger. I don’t understand why if I press the button once, the “got here” message is displayed in the “Button double clicked” if block. Since I’m only pressing the button once, how is it executing anything in the second if statement when the condition is “if triggered by double click”. I don’t see how “got here” is being displayed when I single click.

Note: For some reason when I click the “Copy to clipboard” button it is only copying the first line. I had to select all the text then copy the YAML. Not sure why.

-Thanks

alias: Kitchen Lights Button
description: ""
triggers:
  - device_id: c4dd32bbddf508fd858abb8232285174
    domain: zha
    type: remote_button_short_press
    subtype: button
    trigger: device
    id: button pressed
  - device_id: c4dd32bbddf508fd858abb8232285174
    domain: zha
    type: remote_button_double_press
    subtype: button
    trigger: device
    id: double clicked
conditions: []
actions:
  - if:
      - condition: trigger
        id:
          - button pressed
      - condition: state
        entity_id: light.cync_full_color_undercabinet_18_2
        state:
          - "off"
    then:
      - action: light.turn_on
        metadata: {}
        data: {}
        target:
          entity_id:
            - light.cync_full_color_undercabinet_18_2
            - light.cync_full_color_undercabinet_18
    else:
      - action: light.turn_off
        metadata: {}
        target:
          entity_id:
            - light.cync_full_color_undercabinet_18_2
            - light.cync_full_color_undercabinet_18
        data: {}
  - if:
      - condition: trigger
        id:
          - double clicked
      - condition: state
        entity_id: switch.ceiling_lights
        state:
          - "off"
    then:
      - action: light.turn_on
        metadata: {}
        target:
          entity_id:
            - light.cync_full_color_undercabinet_18_2
            - light.cync_full_color_undercabinet_18
        data: {}
      - action: switch.turn_on
        metadata: {}
        target:
          entity_id: switch.ceiling_lights
        data: {}
    else:
      - action: persistent_notification.create
        metadata: {}
        data:
          message: got here
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          entity_id:
            - light.cync_full_color_undercabinet_18_2
            - light.cync_full_color_undercabinet_18
      - action: switch.turn_off
        metadata: {}
        target:
          entity_id: switch.ceiling_lights
        data: {}
    enabled: true
mode: single

It’s doing the actions in the else block, just like it should.

I was thinking that this if block including the “else” would only be executed if the condition of double click was satisfied. I still don’t get how it is executing the else statement in this block.

-Thanks

I added this conditional after the else to prevent the else part from being executed. I still don’t see why it executes when this if statement is executed via the double click trigger.

condition: state
state:
  - "on"
entity_id: switch.ceiling_lights

You have 2 If/Then actions that are independent of one another. No matter which trigger fires, at least one of the Else clauses will be executed.

If your sets of actions are mutually exclusive, you may be better off using a Choose action, instead of (or in conjunction with) the two If/Thens.

You can do any of the following:

Option 1: Add a condition to each Else
alias: Kitchen Lights Button
description: ""
triggers:
  - device_id: c4dd32bbddf508fd858abb8232285174
    domain: zha
    type: remote_button_short_press
    subtype: button
    trigger: device
    id: button pressed
  - device_id: c4dd32bbddf508fd858abb8232285174
    domain: zha
    type: remote_button_double_press
    subtype: button
    trigger: device
    id: double clicked
conditions: []
actions:
  - if:
      - condition: trigger
        id: button pressed
      - condition: state
        entity_id: light.cync_full_color_undercabinet_18_2
        state: "off"
    then:
      - action: light.turn_on
        target:
          entity_id:
            - light.cync_full_color_undercabinet_18_2
            - light.cync_full_color_undercabinet_18
    else:
      - condition: trigger
        id: button pressed
      - action: light.turn_off
        target:
          entity_id:
            - light.cync_full_color_undercabinet_18_2
            - light.cync_full_color_undercabinet_18
  - if:
      - condition: trigger
        id: double clicked
      - condition: state
        entity_id: switch.ceiling_lights
        state: "off"
    then:
      - action: light.turn_on
        target:
          entity_id:
            - light.cync_full_color_undercabinet_18_2
            - light.cync_full_color_undercabinet_18
      - action: switch.turn_on
        target:
          entity_id: switch.ceiling_lights
    else:
      - condition: trigger
        id: double clicked
      - action: persistent_notification.create
        data:
          message: got here
      - action: light.turn_off
        target:
          entity_id:
            - light.cync_full_color_undercabinet_18_2
            - light.cync_full_color_undercabinet_18
      - action: switch.turn_off
        target:
          entity_id: switch.ceiling_lights
    enabled: true
mode: single
Option 2: If/Thens in a Choose
alias: Kitchen Lights Button
description: ""
triggers:
  - device_id: c4dd32bbddf508fd858abb8232285174
    domain: zha
    type: remote_button_short_press
    subtype: button
    trigger: device
    id: button pressed
  - device_id: c4dd32bbddf508fd858abb8232285174
    domain: zha
    type: remote_button_double_press
    subtype: button
    trigger: device
    id: double clicked
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id: button pressed
        sequence:
          - if:
              - condition: state
                entity_id: light.cync_full_color_undercabinet_18_2
                state: "off"
            then:
              - action: light.turn_on
                target:
                  entity_id:
                    - light.cync_full_color_undercabinet_18_2
                    - light.cync_full_color_undercabinet_18
            else:
              - action: light.turn_off
                target:
                  entity_id:
                    - light.cync_full_color_undercabinet_18_2
                    - light.cync_full_color_undercabinet_18
      - conditions:
          - condition: trigger
            id: double clicked
        sequence:
          - if:
              - condition: state
                entity_id: switch.ceiling_lights
                state: "off"
            then:
              - action: light.turn_on
                target:
                  entity_id:
                    - light.cync_full_color_undercabinet_18_2
                    - light.cync_full_color_undercabinet_18
              - action: switch.turn_on
                target:
                  entity_id: switch.ceiling_lights
            else:
              - condition: trigger
                id: double clicked
              - action: persistent_notification.create
                data:
                  message: got here
              - action: light.turn_off
                target:
                  entity_id:
                    - light.cync_full_color_undercabinet_18_2
                    - light.cync_full_color_undercabinet_18
              - action: switch.turn_off
                target:
                  entity_id: switch.ceiling_lights
mode: single
Option 3: A Choose and a couple templates
alias: Kitchen Lights Button
description: ""
triggers:
  - device_id: c4dd32bbddf508fd858abb8232285174
    domain: zha
    type: remote_button_short_press
    subtype: button
    trigger: device
    id: button pressed
  - device_id: c4dd32bbddf508fd858abb8232285174
    domain: zha
    type: remote_button_double_press
    subtype: button
    trigger: device
    id: double clicked
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id: button pressed
        sequence:
          - action: light.turn_{{ 'on' if is_states('light.cync_full_color_undercabinet_18_2','off') else 'off' }}
            metadata: {}
            data: {}
            target:
              entity_id:
                - light.cync_full_color_undercabinet_18_2
                - light.cync_full_color_undercabinet_18
      - conditions:
          - condition: trigger
            id: double clicked
        sequence:
          - action: homeassistant.turn_{{ 'on' if is_states('switch.ceiling_lights','off') else 'off' }}
            metadata: {}
            target:
              entity_id:
                - light.cync_full_color_undercabinet_18_2
                - light.cync_full_color_undercabinet_18
                - switch.ceiling_lights
mode: single

Thanks, I’ll take a look at this tomorrow.

What do you imagine “else” means?

If I win the lottery
Then cheer and jump around
Else rip the ticket up and throw the bits in the bin

That’s funny! But I thought the difference was that the second “else” was in a completely different “if” block triggered by a different trigger. Evidently it doesn’t work that way.

Thanks again I really appreciate your help with this. I’ve been using the Choose option when multiple triggers were involved. I thought the If/Then was a bit cleaner. Just have to add the conditions and I should be good to go.

And after 4 months of using Home Assistant I thought I knew it all! :slight_smile: