Automation randomly triggers

Hi,
I am using an Aquara mini switch to toggle the lights on/off with an automation.
While the automation works fine, it randomly triggers without anyone pressing the button. It doesn’t seem to trigger when the lights have been turned off via their own application (philips hue) in the first place.

How can I debug/stop this?

Automation looks like this:

alias: Toggle Light Office Yan
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.switch_office_yan_action
condition: []
action:
  - if:
      - condition: device
        type: is_off
        device_id: b1bcaae9b7fdacd502b88617c688c7d1
        entity_id: 0b2d42f4701ea1481cbfe42a825bd850
        domain: light
    then:
      - service: light.turn_on
        target:
          area_id: office
        data: {}
    else:
      - service: light.turn_off
        target:
          area_id: office
        data: {}
mode: single

Thanks!

If you look at the trace from when the automation ran, it will give you all the details. I’m going to guess that the switch briefly went unavailable, so it’s state changed and triggered the automation.

Assuming that’s it, do some searching on the forum; there’s multiple ways to handle this.

Looking at the automation, any state change on the sensor will fire the action. So not just going from unavailable to available, but possibly also attribute changes if there are any (or will be added in the future). Depending on how the button is implemented, you need to look at a specific event that you are interested in (single click, long press,… ). I know too little about the particular switch to know what event you should look for.

Ho I just want to trigger the toggle on a simple button press.
The button is added with zigbee2MQTT and this is the entities that it exposes

image

right now, I link my automation directly to the “Action”, not sure if there is another way to configure this?

change it to this:

trigger:
  - platform: state
    entity_id: sensor.switch_office_yan_action
    to:

that way it only checks the state of the sensor not any of the attributes.

1 Like

Look in the Z2M device page (assuming this one).
You will have the names the action will take when the button is triggered (or in case of keyfobs, to identify multiple buttons).

This way you can identify and react to each action
trigger:
  - platform: state
    entity_id:
      - sensor.switch_office_yan_action
    to: single
    id: SINGLE
  - platform: state
    entity_id:
      - sensor.switch_office_yan_action
    to: double
    id: DOUBLE
  - platform: state
    entity_id:
      - sensor.switch_office_yan_action
    to: hold
    id: HOLD
  - platform: state
    entity_id:
      - sensor.switch_office_yan_action
    to: release
    id: RELEASE
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - SINGLE
        sequence: []
      - conditions:
          - condition: trigger
            id:
              - DOUBLE
        sequence: []
      - conditions:
          - condition: trigger
            id:
              - HOLD
        sequence: []
      - conditions:
          - condition: trigger
            id:
              - RELEASE
        sequence: []

If you want a simple on/off switch without caring how (single, double, hold), for that button you can probably use release only, which should trigger in all cases the same.

alias: Toggle Light Office Yan
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.switch_office_yan_action
    to: release
condition: []
action:
  - service: light.toggle
    data: {}
    target:
      area_id: office
mode: single

The “release” action needed me to hold the button for a second before the release got detected. I changed it to “Single” and it seems to work great so far!

Thanks!

1 Like

Always a quirk with these buttons, you just have to find it! :smile:
Since release doesn’t work as I expected, be careful with only single as trigger, if, by mistake, you were to double-press (not sure how sensitive it is), it won’t trigger, unless you add it.

  - platform: state
    entity_id:
      - sensor.switch_office_yan_action
    to:
      - single
      - double

This will trigger if single-pressed OR double-pressed.

1 Like