Skip to content

Instantly share code, notes, and snippets.

@rhl2401
Last active January 23, 2025 18:30
Show Gist options
  • Save rhl2401/871cdc129c2d61f894407bef4b44f07d to your computer and use it in GitHub Desktop.
Save rhl2401/871cdc129c2d61f894407bef4b44f07d to your computer and use it in GitHub Desktop.
Aqara Wireless Switch (single, double, hold)
blueprint:
name: Aqara Wireless Switch (single, double, hold, release)
description: 'Control anything using Aqara Wireless Switch.
Customizable actions for each press.
This version of the blueprint is for buttons supporting single, double, hold and
release actions. '
domain: automation
input:
action_sensor:
name: Action Sensor
description: Zigbee2mqtt action sensor
selector:
entity:
integration: mqtt
domain: sensor
multiple: false
press_single:
name: Single button press
description: Action to run on single button press
default: []
selector:
action: {}
press_double:
name: Double button press
description: Action to run on double button press
default: []
selector:
action: {}
press_hold:
name: Button hold
description: Action to run on hold
default: []
selector:
action: {}
press_release:
name: Button release
description: Action to run on release
default: []
selector:
action: {}
mode: restart
max_exceeded: silent
trigger:
platform: state
entity_id: !input action_sensor
condition:
- condition: template
value_template: '{{ (trigger.to_state.attributes.action if trigger.to_state.state == ""
else trigger.to_state.state) in ("single","double","hold","release")
}}'
action:
- variables:
command: '{{ trigger.to_state.attributes.action if trigger.to_state.state == ""
else trigger.to_state.state }}'
- choose:
- conditions:
- '{{ command == ''single'' }}'
sequence: !input press_single
- conditions:
- '{{ command == ''double'' }}'
sequence: !input press_double
- conditions:
- '{{ command == ''hold'' }}'
sequence: !input press_hold
- conditions:
- '{{ command == ''release'' }}'
sequence: !input press_release
@ovizii
Copy link

ovizii commented Jun 13, 2022

Hey there, thanks for sharing this blueprint. I have extended it as my WXKG11LM also supports the "release" action in addition to the "hold" action. Give it a try, yours might also support it.

@rhl2401
Copy link
Author

rhl2401 commented Sep 15, 2022

Great, nice to hear and thanks for sharing. I will look into this.

@brianhanifin
Copy link

brianhanifin commented Oct 30, 2022

OK, I have confirmed this condition does stop errant commands from being sent.

A couple of months ago my son claimed his bedside button wouldn't turn off his light anymore. I pushed his button and Home Assistant reported his light turned on, and then off. So declared the button working.

When he brought it to me again a couple of days ago I looked closer and realized that I hadn't watch it long enough. About 30 seconds after it reports "Off" it reverts to an "On" state. When I looked in person I realized his light was turning off, then immediately coming back on again. For some reason I could turn the light on with the remote, but it wouldn't stay off.

I found a discussion on a different blueprint thread where @SirGoodenough recommended adding a condition. Inserting the following condition into your blueprint resolved my issue (although I found I had to delete and recreate the blueprint automation for the local change to be recognized).

condition:
  - condition: template
    value_template: '{{ trigger.to_state.attributes.action in ("single","double","hold") }}'

For reference here is a link to my revision of your blueprint.
https://gist.github.com/brianhanifin/7365667417d255ffcf9a6283714de8db

@rhl2401
Copy link
Author

rhl2401 commented Oct 31, 2022

Thanks, @brianhanifin
I have updated the gist and it should be pushed to HA shortly.

Have a great day!

@CrazyCoder
Copy link

CrazyCoder commented Nov 27, 2022

It seems to be broken with this condition, should be:

condition:
  - condition: template
    value_template: '{{ trigger.to_state.state in ("single","double","hold") }}'

@rhl2401
Copy link
Author

rhl2401 commented Dec 10, 2022

It has been updated and should work now. Thanks @CrazyCoder

@ovizii
Copy link

ovizii commented Jan 19, 2023

I made some changes in Z2M and now your blueprint does not work for me anymore. I had actually an older version of your blueprint, Instead of input: action_sensor: I think my copy used a switch as input. Anyway, this current version here does not "see" my WXKG11LM

I click "create automation" next to your blueprint and it cannot find any matching entity. If I type my friendly name of my WXKG11LM I am only presented with these entities:

image

Any idea what the problem could be? The changes I made to Z2M were to turn off all legacy options I could find. For mqtt as well as for HA and the WXKG11LM has it legacy option turned off:
image

@Estradamis
Copy link

Is there a possibility to update this to the new z2m 2.0 version?

@rhl2401
Copy link
Author

rhl2401 commented Jan 5, 2025

@Estradamis
I'm no longer working on this blueprint as I no longer have the Aquara button, thus no way to test.
If anyone can commit an update to make this compatible with Z2M 2.0, I will gladly accept and implement the changes.

@denselm
Copy link

denselm commented Jan 6, 2025

It looks like @ovizii came across the same breaking changes voluntarily that Z2M 2.0 now enforces. @Estradamis, @rhl2401, I have forked this gist and refactored it to use MQTT device triggers as per the Z2M2.0 upgrade recommendations at Koenkk/zigbee2mqtt#24198.

The updated gist can be found at https://gist.github.com/denselm/5049daf0d2812c8b6ae9a6dc298f471c

@stncttr908
Copy link

@denselm
Copy link

denselm commented Jan 6, 2025

Unfortunately, it doesn't look like MQTT device triggers support templates so I suspect supporting multiple devices in trigger an action in the same automation will require a refactor to use MQTT topic triggers and parse out the triggered device ids instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment