Simple automation with zigbee2mqtt do not works

I would like to use this automation to turn on/off my Xiaomi Yeelight with a Aqara Switch button… but it do not works… why?

  - alias: Switch Yeelight
    trigger:
      platform: mqtt
      topic: 'zigbee2mqtt/sensor.0x00158d0001593043_click'
    condition:
      condition: template
      value_template: "{{ 'single' == trigger.payload_json.click }}"
    action:
      service: light.toggle
      entity_id: light.yeelightw1

Double check the topic is correct to start with by watching the zigbee2mqtt log or subscribing with mqtt_sub and check that an event is received when you push the button

When I click the xiaomi button the addon zigbee2mqtt log show:

zigbee2mqtt:info 2019-1-22 23:20:58 MQTT publish: topic ‘zigbee2mqtt/0x00158d0001593043’, payload ‘{“click”:“single”,“linkquality”:28,“last_seen”:“2019-01-22T22:20:58.654Z”}’

but nothing happens

I’m afraid, the mqtt trigger is not flexible enough for your approach. Would you please try the following (adjust to your needs) setup?

For the sensor:

sensor:
  - platform: mqtt
    name: "aqara_switch_button"
    state_topic: "zigbee2mqtt/0x00158d0001593043"
    value_template: '{{value_json.click}}'
    json_attributes_topic: "zigbee2mqtt/0x00158d0001593043"

And for the trigger part of your automation:

trigger:
  platform: state
  entity_id: sensor.aqara_switch_button
  to: 'single'

No condition needed.

I solved in another way…
Since my Hassio discover Xiaom switch and call it “sensor.0x00158d0001593043_click”
I created a new automation…

  - alias: Turn_on_off_yeelight
    trigger:
      platform: state
      entity_id: sensor.0x00158d0001593043_click
      to: 'single'
    action:
      service: light.toggle
      entity_id: light.yeelightw1

and it works fine!

Is it correct?

2 Likes

@vpomax I believe that there is an issue with using an automation like that. I dont think it will trigger if you do 2 single clicks as it wont be change TO single (it will already be single)

This is the reason to use an automation like this:

 - alias: "Adam Light Off"
   trigger:
     - platform: mqtt
       topic: 'zigbee2mqtt/Adams_Switch'
     - platform: mqtt
       topic: 'zigbee2mqtt/Bedroom_Main_Switch'
   condition:
     condition: and
     conditions:
      - condition: template
        value_template: "{{ 'single' == trigger.payload_json.click }}"
      - condition: state
        entity_id: light.adams_light_light
        state: 'on'
   action:
     - service: homeassistant.turn_off
       entity_id: light.adams_light_light
1 Like

Nice concept. I tried it for myself and am really impressed. Didn’t know of mqtt triggers before. Thanks a lot for sharing this.

Here’s how I got it to toggle a light (changing the ‘single’ to ‘double’ or ‘hold’ also works):

- id: switch_click
  alias: Switch click
  hide_entity: true
  trigger:
    platform: state
    entity_id: sensor.0x0011110101111111_click
    to: 'single'
  action:
  - service: light.toggle
    data:
      entity_id: light.light_name
1 Like