Skip to content

Instantly share code, notes, and snippets.

@rhl2401
Last active March 20, 2024 11:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • 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
@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

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