Skip to content

Instantly share code, notes, and snippets.

@brianhanifin
Last active September 16, 2019 03:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianhanifin/1a4baec7459249772a0318902c9a86cf to your computer and use it in GitHub Desktop.
Save brianhanifin/1a4baec7459249772a0318902c9a86cf to your computer and use it in GitHub Desktop.
Home Assistant: Xiaomi Button Click Event Handler
alias: xiaomi_button_click
initial_state: on
trigger:
platform: event
event_type: xiaomi_aqara.click
action:
#click_types: single, double, long, both, double_both, long_both
- service: script.xiaomi_button_handler
data_template:
button: >
{{ trigger.event.data.entity_id }}
type: >
{{ trigger.event.data.click_type }}
xiaomi_button_handler:
sequence:
## Enable the following to display the button was clicked and the click type.
##
# - service: persistent_notification.create
# data_template:
# title: "xiaomi_button_handler"
# message: "button: {{ button }}\ntype: {{ type }}\nscript.{{ button|replace('binary_sensor.','') }}"
- service_template: >
{# Dog's Medicine Button #}
{%- if button == "binary_sensor.medicine_buddy_button" -%}
script.medicine_buddy_button
{# Bedroom Wall Double Switch #}
{%- elif button.startswith("binary_sensor.bedroom_switch_button") -%}
script.bedroom_switch_buttons
{%- endif %}
data_template:
type: "{{ type }}"
side: "{{ button|replace('binary_sensor.bedroom_switch_button_','') }}"
bedroom_switch_buttons:
sequence:
# - service: persistent_notification.create
# data_template:
# title: "bedroom_switch_buttons"
# message: "type: {{ type }}\nside: {{ side }}"
- service_template: >
{%- if type in ["long", "double", "long_both", "double_both"] -%}
switch.toggle
{%- elif type in ["single", "both"] -%}
light.toggle
{%- endif %}
data_template:
entity_id: >
{%- if type in ["long_both", "double_both"] -%}
switch.sound_machine, switch.bedroom_fan
{%- elif type in ["long", "double"] -%}
{%- if side == "left" -%}
switch.sound_machine
{%- elif side == "right" -%}
switch.bedroom_fan
{%- endif -%}
{%- elif type == "both" -%}
light.bedroom, light.brian
{%- elif type == "single" -%}
{%- if side == "left" -%}
light.bedroom
{%- elif side == "right" -%}
light.brian
{%- endif -%}
{%- endif %}
medicine_buddy_button:
sequence:
# - service: persistent_notification.create
# data_template:
# title: "medicine_buddy_button"
# message: "type: {{ type }}"
- service: logbook.log
data_template:
name: "Buddy's Medicine"
message: >
{# single #}
{%- if type in ["single", "long"] -%}
"was given"
{# double #}
{%- else -%}
"was cleared"
{%- endif %}
# Update old sensor
- service: mqtt.publish
data_template:
topic: "homeassistant/medicine/buddy/state"
payload: >-
{%- if type in ["single", "long"] -%}
{{ as_timestamp(now())|timestamp_custom('%Y-%m-%d') }}
{%- endif %}
retain: true
- service: mqtt.publish
data_template:
topic: "homeassistant/medicine/buddy/attributes"
payload: >-
{
"day": "{{ as_timestamp(now())|timestamp_custom('%A') }}",
"updated": "{{ as_timestamp(now())|timestamp_custom('%B %-d %Y %-I:%M %P') }}"
}
retain: true
# Update check-button-card sensor
- service: mqtt.publish
data_template:
topic: homeassistant/sensor/medicine_buddy_taken/state
payload: >-
{
"timestamp": {{ as_timestamp(now())|int }},
"visibility_timeout": "none",
"visible": true,
"unit_of_measurement": "timestamp",
"version": "0.1.0"
}
retain: true
@brianhanifin
Copy link
Author

I wanted a way to capture Xiaomi button events and to hide the complexity away from my task oriented scripts. After watching DrZzs (@snipercanine) Xiaomi Aquare Smart Home Kit video I decided to share my solution with everyone.

If you are interested in seeing my full Home Assistant repository go here: https://github.com/brianhanifin/Home-Assistant-Config/

Ignore #4. I have included it for completeness, but it is more confusing that is necessary. I'm using a custom lovelace button to keep track of when chores get done and it integrates with MQTT. Ask if you'd like to know more about it.

@johan
Copy link

johan commented Sep 15, 2019

Thanks! I'm curious about your MQTT integration too; it sounds fun. 🤠

@brianhanifin
Copy link
Author

@johan I have used this custom lovelace button to track when chores were last some. It stores the timestamp in mqtt. I stuck a Xiaomi pushbutton inside the kitchen cabinet door next to the dog's medicine. Clicking the physical button updates the lovelace check button card sensor, as if I clicked the virtual button on my phone!

https://community.home-assistant.io/t/lovelace-check-button-card/92980

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