Best way to write automations for zigbee2mqtt

Hi,
i switched all my devices from a ZHA / deconz combination to zigbee2mqtt for better interface and control reasons.
Writing automations is a bit cumbersome though, because getting the device ids is more difficult as i cannot use the event listener method as i did with deconz or zha events. But maybe i am wrong and there is a method working the same way for mqtt. So feel free to let me know if you have a more efficient method.

To get the device ids i currently use HAs built in automation tool to build the automation i want and then i move the device id part from automations.yaml to my own yaml file.

I only use state triggers or mqtt triggers with Zigbee2MQTT, so I never need device id’s.

Maybe you can give me an example.

This is a trigger that i copied from HAs automation builder:

trigger:
- platform: device
  domain: mqtt
  device_id: 55bdb682ddc06af3598bbefa66e1839f
  type: action
  subtype: off_press
  discovery_id: 0x001788010926b993 action_off_press

Mqtt trigger :

- id: '1567689190101'
  alias: bedlicht aan
  trigger:
  - platform: mqtt
    topic: zigbee2mqtt/ikea_button1/action
    payload: 'on'
  action:
  - data:
      entity_id: light.light_ikea1
    service: light.turn_on
  mode: single

state trigger :

- id: '1567689065684'
  alias: Licht badkamer aan
  description: Licht badkamer aan
  trigger:
  - entity_id: binary_sensor.ikeapir_occupancy
    platform: state
    to: 'on'
  condition:
  - condition: template
    value_template: '{{ state_attr("sun.sun", "elevation") < 5 }}'
  action:
  - data:
      entity_id: light.lichtbadkamer
    service: light.turn_on
  mode: restart
1 Like

Perfect, thank you !