Physical Buttons for Home Assistant

Per @ciotlosm input from 4 months back, zigbee2mqtt really is a great and cheap way to add buttons and sensors.
I just finished to migrate my devices from xiaomi hub to zigbee2mqtt (sadly prompted by a dataloss event :frowning: )

Thanks to zigbee2mqtt the Xiaomi buttons are as capable as the button itself will support.
Also the philips hue dimmer switch is awesome - its a 4 button device and each button supports click(s) with count of clicks, long click, long release etc and its very easily available at lowish cost.
MANY other sensors, switches and buttons are also supported and the list is growing.

Full list here:

I have finally received my buttons and set up opennqtt gateway… I had to change the pin on my nodemcu to get things working with the RF rx I bought.

My issue now is the RF buttons appear to give a different code each time they are clicked! I see this in my mqtt broker and the value that Homeassistant shows in the front end!

Here is a sample from my mqtt broker - this is pressing two buttons:

home/OpenMQTTGateway/433toMQTT {"value":14489825,"protocol":1,"length":24,"delay":277}
home/OpenMQTTGateway/433toMQTT {"value":4074129,"protocol":1,"length":24,"delay":293}
home/OpenMQTTGateway/433toMQTT {"value":4139665,"protocol":1,"length":24,"delay":293}
home/OpenMQTTGateway/433toMQTT {"value":6109217,"protocol":11,"length":23,"delay":428}
home/OpenMQTTGateway/433toMQTT {"value":6105313,"protocol":11,"length":23,"delay":425}
home/OpenMQTTGateway/433toMQTT {"value":4139665,"protocol":1,"length":24,"delay":299}
home/OpenMQTTGateway/433toMQTT {"value":4139153,"protocol":1,"length":24,"delay":293}
home/OpenMQTTGateway/433toMQTT {"value":14489825,"protocol":1,"length":24,"delay":278}
home/OpenMQTTGateway/433toMQTT {"value":4131473,"protocol":1,"length":24,"delay":292}
home/OpenMQTTGateway/433toMQTT {"value":4139153,"protocol":17,"length":24,"delay":395}
home/OpenMQTTGateway/433toMQTT {"value":14495969,"protocol":1,"length":24,"delay":272}
home/OpenMQTTGateway/433toMQTT {"value":6109217,"protocol":11,"length":23,"delay":426}
home/OpenMQTTGateway/433toMQTT {"value":14485729,"protocol":17,"length":24,"delay":368}
home/OpenMQTTGateway/433toMQTT {"value":4139665,"protocol":1,"length":24,"delay":299}
home/OpenMQTTGateway/433toMQTT {"value":4131473,"protocol":1,"length":24,"delay":293}
home/OpenMQTTGateway/433toMQTT {"value":4137617,"protocol":1,"length":24,"delay":293}
home/OpenMQTTGateway/433toMQTT {"value":14498017,"protocol":1,"length":24,"delay":273}
home/OpenMQTTGateway/433toMQTT {"value":14495969,"protocol":1,"length":24,"delay":273}
home/OpenMQTTGateway/433toMQTT {"value":4139665,"protocol":1,"length":24,"delay":298}

Any ideas?

John

Is it the same set of codes repeated over and over? Probably

Hi all

Any ideas what is simplest way get wireless buttons to HASS.io with minimal work. I have couple zwave wireless buttons and i hate that network and add system to zwave. Be so automatic, but newer find anything :slight_smile:

How about this Xiaomi buttons, need some gateway that too?
https://www.aliexpress.com/item/2018-Upgraded-Xiaomi-Aqara-Wireless-Switch-Single-Double-Button-Key-Smart-Light-Control-ZigBee-Version-for/32863511201.html

Happily take every tips/hints what is best and EASYEST way to get working simple wireless buttons. All other nodes working with mqtt, but they use wall sockets and need couple places wireless buttons without wires, that why need some easy and simple buttons.

Many thanks every answers.

I started this thread over 2 years ago! I use a Xiaomi smart hub with a couple of their buttons around the house. Buttons are simple (click, double click, long press) and can be bound to automations in minutes. I have had them in for nearly 2 years now and i haven’t had to change the batteries. I even use one of the buttons on my front door as my doorbell now (Xiaomi smart hub has a speaker in it which can play from a range of sounds or you can upload any mp3) and the automation also sends me a push notification via the alexa app and trips all the alexas in the house to say “There is someone at the door”. Love the Xiaomi kit, i have pick up some temp sensors and motion sensors also to go with the buttons. Super cheap platform too. 11 out of 10 :wink:

~Mick

This is an example of the kjt that I have, but you can buy all the parts seperately if you don’t want the full kit. Search for Xiaomi Mijia.

@Mick_Kerrigan Can you please share one or two of your simplest automations using the Xiaomi smart switches? I’m just getting these setup this evening, and an example would be MOST helpful. Thank you!

I can help if you like. Now this may seem far from simple, however if you mainly just look at the first automation section where I distinguish between the 3 types of clicks and what to do based on each type, you will see how capable these little switches are

I use packages so all this is in a package called ‘kitchen_light.yaml’

#  All code relating to the functioning of the kitchen light

input_number:
  kitchen_light_auto_off_time:
    name: Off after
    min: 1
    max: 9
    step: 1
    icon: mdi:camera-timer
    unit_of_measurement: min

  kitchen_min_lux:
    name: On below
    min: 0
    max: 30
    step: 1
    icon: mdi:brightness-6
    unit_of_measurement: lux


automation:
  - alias: 'Kitchen Remote Button 1'
    initial_state: 'on'
    trigger:
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d0002241b37
          click_type: single
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d0002241b37
          click_type: double
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d0002241b37
          click_type: long_click_press
    action:
      - service_template: >
          {% if trigger.event.data.click_type == 'single' %}
            script.kitchen_button_single_click
          {% elif trigger.event.data.click_type == 'double' %}
            script.kitchen_button_double_click
          {% elif trigger.event.data.click_type == 'long_click_press' %}
            script.kitchen_button_long_press
          {% endif %}
          

  - alias: 'Kitchen light auto ON motion'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.kitchen_multi_sensor_sensor
        to: 'on'
    condition:      # look at changing this to use room light level only
      condition: or
      conditions:
#      - condition: sun
#        after: sunset
#        after_offset: '-00:15:00'
#      - condition: sun
#        before: sunrise
#        before_offset: '-01:00:00'
      - condition: template
        value_template: "{{ states('sensor.kitchen_multi_sensor_luminance') | int < states('input_number.kitchen_min_lux') | int }}"
      - condition: state
        entity_id: light.kitchen_light_level
        state: 'on'
    action:
      - service: script.turn_off
        entity_id: script.kitchen_light_auto_off_timer
      - service_template: >
          {% set t = now().strftime('%T') %}
          switch.turn_o{{ 'n' if t > '20:00:00' or t < '06:30:00' else 'ff' }}
        entity_id: 
          - switch.circadian_lighting_kitchen_circadian_lighting
      - service: light.turn_on
        data:
          entity_id: light.kitchen_light_level
      - condition: and
        conditions: 
          - condition: time
            before: '20:00:00'
          - condition: time
            after: '06:30:00'
      - service: light.turn_on
        data:
          entity_id: light.kitchen_light_level
          brightness: 255

  - alias: 'Kitchen light auto off'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.kitchen_multi_sensor_sensor
        to: 'off'
    action:
      - service: script.turn_off
        entity_id: script.kitchen_light_auto_off_timer
      - service: script.turn_on
        entity_id: script.kitchen_light_auto_off_timer


script:
  kitchen_button_single_click:
    alias: Kitchen button single click
    sequence:
    - service: automation.turn_off
      data:
        entity_id: 
          - automation.kitchen_light_auto_on_motion
          - automation.kitchen_light_auto_off
    - service: script.turn_off
      entity_id: script.kitchen_light_auto_off_timer
    - service: switch.turn_off
      data:
        entity_id: switch.circadian_lighting_kitchen_circadian_lighting
    - service: light.turn_on
      data:
        entity_id: light.kitchen_light_level
        brightness: 255

  kitchen_button_double_click:
    alias: Kitchen button double click
    sequence:
    - service: automation.turn_off
      data:
        entity_id: 
          - automation.kitchen_light_auto_on_motion
          - automation.kitchen_light_auto_off
    - service: light.turn_off
      data: 
        entity_id: light.kitchen_light_level

  kitchen_button_long_press:
    alias: Kitchen button long press
    sequence:
    - service: automation.turn_on
      data:
        entity_id: 
          - automation.kitchen_light_auto_on_motion
          - automation.kitchen_light_auto_off

  kitchen_light_auto_off_timer:
    alias: Kitchen light off after preset time
    sequence:
    - delay: '00:0{{ states.input_number.kitchen_light_auto_off_time.state | int }}:00'
    - service: homeassistant.turn_on
      data:
        entity_id: light.kitchen_light_level
        brightness: 60
    - delay: '00:00:03'
    - service: homeassistant.turn_on
      data:
        entity_id: light.kitchen_light_level
        brightness: 30
    - delay: '00:00:02'
    - service: homeassistant.turn_off
      data:
        entity_id: light.kitchen_light_level

MUCH appreciated, just a quick look showed me a few things I can change/try. THANK YOU!! :smiley:

Most specifically, the fully-qualified event name xiaomi_aqara.click was the key item. It allowed me to listen for the events of each switch I’ve added to the new gateway and confirm the proper entity_id for each one. The automations I started with are now working, after only a very few minor changes. THANKS!! :smiley: