#If/Then not working on button event_type trigger

1 messages · Page 1 of 1 (latest)

dusk zodiac
#
description: ""
triggers:
  - device_id: e364fdb224cf89cf475d56ed85afb52d
    domain: homekit_controller
    type: button1
    subtype: long_press
    trigger: device
    id: Long
  - trigger: state
    entity_id:
      - event.ceiling_fan_button_button_6
    attribute: event_type
    to: multi_press_1
    id: Single
  - alias: Double press button
    trigger: state
    entity_id:
      - event.ceiling_fan_button_button_6
    attribute: event_type
    to: multi_press_2
    id: Double
  - alias: Long press button
    trigger: state
    entity_id:
      - event.ceiling_fan_button_button_6
    attribute: event_type
    to: long_press
    id: Long
conditions: []
actions:
  - parallel:
      - alias: If single press, set fan mode to 1 (if off or other modes)
        if:
          - condition: and
            conditions:
              - condition: trigger
                id: Single
              - condition: or
                conditions:
                  - condition: state
                    entity_id: input_select.fan_mode
                    state: "Off"
                  - condition: state
                    entity_id: input_select.fan_mode
                    state: "2"
                  - condition: state
                    entity_id: input_select.fan_mode
                    state: "3"
        then:
          - action: scene.turn_on
            target:
              entity_id: scene.fan_1
            data: {}
      - alias: If double press, set fan mode to 2 (if off or other modes)
        if:
          - condition: and
            conditions:
              - condition: trigger
                id: Double
              - condition: or
                conditions:
                  - condition: state
                    entity_id: input_select.fan_mode
                    state: "Off"
                  - condition: state
                    entity_id: input_select.fan_mode
                    state: "1"
                  - condition: state
                    entity_id: input_select.fan_mode
                    state: "3"
        then:
          - action: scene.turn_on
            target:
              entity_id: scene.fan_2
            data: {}
      - alias: If long press, set fan mode to 3 (if off or other modes)
        if:
          - condition: and
            conditions:
              - condition: trigger
                id: Long
              - condition: or
                conditions:
                  - condition: state
                    entity_id: input_select.fan_mode
                    state: "Off"
                  - condition: state
                    entity_id: input_select.fan_mode
                    state: "1"
                  - condition: state
                    entity_id: input_select.fan_mode
                    state: "2"
        then:
          - action: scene.turn_on
            target:
              entity_id: scene.fan_3
            data: {}
      - alias: If corresponding press happens on active mode, turn off the fan
        if:
          - condition: or
            conditions:
              - condition: and
                conditions:
                  - condition: state
                    entity_id: input_select.fan_mode
                    state: "3"
                  - condition: trigger
                    id: Long
              - condition: and
                conditions:
                  - condition: state
                    entity_id: input_select.fan_mode
                    state: "2"
                  - condition: trigger
                    id: Double
              - condition: and
                conditions:
                  - condition: state
                    entity_id: input_select.fan_mode
                    state: "1"
                  - condition: trigger
                    id: Single
        then:
          - action: scene.turn_on
            target:
              entity_id: scene.fan_off
            data: {}
mode: single
#

I am currently working on this automation. Since recently Aqara scenes work with my HA. I am trying to make a button function with IR ceiling fan, which in itself I already got working within the Aqara app.
Now I am trying to reimplement it within home assistant so I have full on tracking in the app. (see image)

Now this automation works fully as intended, except the final bit.

alias: If corresponding press happens on active mode, turn off the fan
if:
  - condition: or
    conditions:
      - condition: and
        conditions:
          - condition: state
            entity_id: input_select.fan_mode
            state: "1"
          - condition: trigger
            id: Single
      - condition: and
        conditions:
          - condition: state
            entity_id: input_select.fan_mode
            state: "2"
          - condition: trigger
            id: Double
      - condition: and
        conditions:
          - condition: state
            entity_id: input_select.fan_mode
            state: "3"
          - condition: trigger
            id: Long
then:
  - action: scene.turn_on
    target:
      entity_id: scene.fan_off
    data: {}

I was assuming this would turn off the ceiling fan when the corrosponding mode to the button press is pressed again. But for some reason it only works on long press. Turning on, or switching modes works just fine though.

ocean holly
#

why are you checking every possible combination?

just check if the fan is "Not" "Off" and if true then turn off

dusk zodiac
#

because I also want to be able to switch from mode 1 to mode 2, if I double press, for example

#

so if the button press corrosponding with mode is on it should turn the fan off again

ocean holly
#

if fan not off
if single press
turn off fan
else if double press
set fan to mode 2

dusk zodiac
#

so essentially, add an else to the first three options instead of doing the fourth option?

ocean holly
#

maybe i am missing stuff but it looks like you may have overcomplicated it

dusk zodiac
#

Well, it's odd. It just seems to be completely ignoring - condition: and conditions: - condition: state entity_id: input_select.fan_mode state: "1" - condition: trigger id: Single - condition: and conditions: - condition: state entity_id: input_select.fan_mode state: "2" - condition: trigger id: Double

#

the rest works just fine

#

Do I need to even surround the AND conditions in an OR condition?

ocean holly
#

yes, i think your currently requiring 2 comflictingg states

#

state cant be both 1 and 2

dusk zodiac
#

Well essentially it's asking if
either

        conditions:
          - condition: state
            entity_id: input_select.fan_mode
            state: "1"
          - condition: trigger
            id: Single

is true
or

      - condition: and
        conditions:
          - condition: state
            entity_id: input_select.fan_mode
            state: "2"
          - condition: trigger
            id: Double

is true
or

      - condition: and
        conditions:
          - condition: state
            entity_id: input_select.fan_mode
            state: "3"
          - condition: trigger
            id: Long

is true

#

but it's only looking at the last for some reason

ocean holly
#

i am struggling to wrap my head about the sea of if statements

dusk zodiac
#

Hmm to add some context. The image showing is a button row of scenes controlling the ceiling fan. (Off/Speed 1/Speed 2/Speed 3)

I then have an automation tracking the scenes.

alias: Track Fan Scene Selection
description: ""
triggers:
  - entity_id:
      - scene.fan_off
      - scene.fan_1
      - scene.fan_2
      - scene.fan_3
    trigger: state
actions:
  - data:
      entity_id: input_select.fan_mode
      option: >
        {% if trigger.entity_id == 'scene.fan_off' %} Off {% elif
        trigger.entity_id == 'scene.fan_1' %} 1 {% elif trigger.entity_id ==
        'scene.fan_2' %} 2 {% elif trigger.entity_id == 'scene.fan_3' %} 3 {%
        endif %}
    action: input_select.select_option
mode: queued
#

Then in the automation I am having issues with it looks for what input_select.fan_mode is set to and looking for the button press action corresponding with that mode.

ocean holly
#

moment let me see if i can suggest a better framework

dusk zodiac
#

alrighty

#

this is the card i am using

type: horizontal-stack
cards:
  - type: custom:button-card
    entity: scene.fan_off
    name: ""
    tap_action:
      action: call-service
      service: scene.turn_on
      service_data:
        entity_id: scene.fan_off
    styles:
      card:
        - background-color: >
            [[[ return (states['input_select.fan_mode'].state == 'Off') ?
            'lightblue' : 'gray'; ]]]
      icon:
        - color: >
            [[[ return (states['input_select.fan_mode'].state == 'Off') ?
            'white' : 'red'; ]]]
        - font-size: 35px
      name:
        - display: none
  - type: custom:button-card
    entity: scene.fan_1
    name: ""
    tap_action:
      action: call-service
      service: scene.turn_on
      service_data:
        entity_id: scene.fan_1
    styles:
      card:
        - background-color: >
            [[[ return (states['input_select.fan_mode'].state == '1') ?
            'lightblue' : 'gray'; ]]]
      icon:
        - color: >
            [[[ return (states['input_select.fan_mode'].state == '1') ? 'white'
            : 'red'; ]]]
        - font-size: 35px
      name:
        - display: none
  - type: custom:button-card
    entity: scene.fan_2
    name: ""
    tap_action:
      action: call-service
      service: scene.turn_on
      service_data:
        entity_id: scene.fan_2
    styles:
      card:
        - background-color: >
            [[[ return (states['input_select.fan_mode'].state == '2') ?
            'lightblue' : 'gray'; ]]]
      icon:
        - color: >
            [[[ return (states['input_select.fan_mode'].state == '2') ? 'white'
            : 'red'; ]]]
        - font-size: 35px
      name:
        - display: none
  - type: custom:button-card
    entity: scene.fan_3
    name: ""
    tap_action:
      action: call-service
      service: scene.turn_on
      service_data:
        entity_id: scene.fan_3
    styles:
      card:
        - background-color: >
            [[[ return (states['input_select.fan_mode'].state == '3') ?
            'lightblue' : 'gray'; ]]]
      icon:
        - color: >
            [[[ return (states['input_select.fan_mode'].state == '3') ? 'white'
            : 'red'; ]]]
        - font-size: 35px
      name:
        - display: none
ocean holly
#

okay its missing the actions but this kind of framework may be easier to work with

#

add that to a new automation and look how how it flows

#

it looks at the current mode and then what button was pressed and you can fill in the actions in the right palce

austere turtle
#
  - trigger: state
    entity_id:
      - event.ceiling_fan_button_button_6
    attribute: event_type
    to: multi_press_1
    id: Single
    alias: Single Press Button

This is a bad trigger for event entities, as it won't retrigger if you do a single press after a single press.

ocean holly
#

updaate the trigger as you need too

dusk zodiac
#

Oh interesting, so it only is working on the third one because it also has long press release as an action

#

I didn't even think of that

austere turtle
#

it'll only trigger when the type changes, not when you get two events of the same type

dusk zodiac
dusk zodiac
austere turtle
#

Just trigger on the event state, not the type

#

use conditions to refine if necessary

dusk zodiac
#

I see

ocean holly
#

you may have to update triggers and stuff, i did the best i could without having your exact setup

austere turtle
#

choose on the event_type instead of trigger_id

dusk zodiac
#

Thanks very much! That was indeed the issue. Triggering it with the event rather than the change of event_type then looking for the event_type after it's triggered seems to have fixed all the issues.
For people looking for something similar in the future. This is the final automation.

alias: Ceiling Fan Button (New)
description: ""
trigger:
  - platform: event
    event_type: state_changed
    event_data:
      entity_id: event.ceiling_fan_button_button_6
condition: []
action:
  - variables:
      press_type: "{{ trigger.event.data.new_state.attributes.event_type }}"

  - choose:
      - alias: "Single Press → Set Fan Mode 1 (if off or other modes)"
        conditions:
          - condition: template
            value_template: "{{ press_type == 'multi_press_1' }}"
          - condition: template
            value_template: "{{ states('input_select.fan_mode') in ['Off', '2', '3'] }}"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.fan_1

      - alias: "Double Press → Set Fan Mode 2 (if off or other modes)"
        conditions:
          - condition: template
            value_template: "{{ press_type == 'multi_press_2' }}"
          - condition: template
            value_template: "{{ states('input_select.fan_mode') in ['Off', '1', '3'] }}"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.fan_2

      - alias: "Long Press → Set Fan Mode 3 (if off or other modes)"
        conditions:
          - condition: template
            value_template: "{{ press_type == 'long_press' }}"
          - condition: template
            value_template: "{{ states('input_select.fan_mode') in ['Off', '1', '2'] }}"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.fan_3

      - alias: "If Corresponding Press Happens on Active Mode → Turn Off Fan"
        conditions:
          - condition: or
            conditions:
              - condition: and
                conditions:
                  - condition: template
                    value_template: "{{ press_type == 'multi_press_1' }}"
                  - condition: state
                    entity_id: input_select.fan_mode
                    state: "1"
              - condition: and
                conditions:
                  - condition: template
                    value_template: "{{ press_type == 'multi_press_2' }}"
                  - condition: state
                    entity_id: input_select.fan_mode
                    state: "2"
              - condition: and
                conditions:
                  - condition: template
                    value_template: "{{ press_type == 'long_press' }}"
                  - condition: state
                    entity_id: input_select.fan_mode
                    state: "3"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.fan_off
mode: single