Triggering automation when state doesn't change - Aqara wireless switch on matter

Hi all,

In short, I am looking to trigger an automation using my aqara wireless remote switch H1 (WRS-RO2). A very basic automation (or so I assumed), press one of the buttons once - toggle the lights on/off.

An event is logged every time the button is pressed, without fail.The below is the event that is registered when the button is pressed once;

event_type: state_changed
data:
  entity_id: event.aqara_wireless_remote_switch_button_2
  old_state:
    entity_id: event.aqara_wireless_remote_switch_button_2
    attributes:
      event_types:
        - multi_press_1
        - multi_press_2
        - long_press
        - long_release
      event_type: multi_press_1
      previousPosition: 1
      totalNumberOfPressesCounted: 1

So setting up a simple automation trigger like the below I assumed this would work.

Unfortunately, it did not trigger every time… not so simple. So after a bit of investigation I can see the device state will always be maintained. So the automation does trigger If the prior state is different. So if I change the state by say doing a double press first, followed by a a single press the automation triggers. Ok I thought, the automation needs a state change or I need a trigger that works on repeat events.

This is where I have been for a couple days now. I’ve tried all sorts of trigger type combinations to no success. I’m very new to HA and still getting my head around how everything works so apologies in advance if this question is pretty basic. I’ve had a good search of the forum and can’t quite see topics which cover my case. As most related to aqara are using them through a separate zigbee controller. All and any help is greatly appreciated.

For some more context;

The switch is connected via a Aqara hub Cluster - M3 as master, M2 child that the switch is linked to. Both bridges are integrated into HA through matter. I’m doing it this way as I invested in the Aqara products a while ago and over a few years through homekit. They work great that way, but I also have a lot of other smart devices accumulated over the years. So I took the plunge into HA with the hope of having a one stop shop for everything to save the annoyance of multiple apps to control. Loving HA so far but this one has me scratching my head an I have a bunch of these switches. I can still use them through the Aqara app but would love if I could get this to work in HA so I can open up there use with non-aqara devices.

Hello Will Landry,

Did you look at event triggers?
Automation Trigger - Home Assistant.

Thank you for your reply and Apologies for taking so long to respond. Honestly in my free time since reading your reply I’ve been trying to read up more on the link you sent and experiment with event triggers before replying. Father of 2 toddlers so free time isn’t something I have much of!

My conclusion, I have no idea what I’m doing. Zero coding or YAML experience, can somewhat understand when I read it but constructing it myself is another story. So figure that’s my next area to improve. I’d imagine from reading, event triggers would solve this but I just have no idea how to put a working one together in YAML.

If you have any links to good sources where I could possibly Frankenstein an example event trigger to work for me I’d greatly appreciate it. Or good source to start learning YAML. I’d imagine that can be found somewhere in the extensive HA documentation though.

Thanks again for replying, I greatly appreciate it.

Looking somemore. Will try out what’s suggested here

Figured it out. Sharing for anyone else that might have a similar question in the future. Pretty basic, should have caught it sooner.
Got it working eventually using Event triggers but in reality the key was having an “and if” condition related to the specific state.

In short, you trigger is based on the entity state change, if a specific state is recorded. In this case “multi_press_1” for a single press. See image. Hopefully, this might save others some time

2 Likes

You saved me a lot of time with your solution. Common sense, but not clear to see. Thanks

1 Like