thegeeklab/content/posts/2022/use-tradfri-shortcut-button-with-homeassistant/index.md
Robert Kaussow 24022c4ee6
All checks were successful
continuous-integration/drone/push Build is passing
post: Store Terraform State on Backblaze S3
2022-09-04 22:27:08 +02:00

4.5 KiB

title date authors tags resources
Use Tradfri Shortcut Button with Home Assistant 2022-08-22T21:35:25+02:00
robert-kaussow
Automation
name src params
feature images/feature.jpg
anchor credits
Center [Artem Bryzgalov](https://unsplash.com/@abrizgalov) on [Unsplash](https://unsplash.com/s/photos/switch)

Sometimes it can be helpful if single actions or entire automation in your Home Automation can be triggered with a physical button and not only from the Web UI. I'm using Zigbee2MQTT as a generic Zigbee to MQTT bridge as it supports a lot of different Zigbee devices and integrates flawlessly into Home Assistant. As I have a lot of different Tradfri Lamps in use already, I have bought a few of the Tradfri Shortcut Button (E1812).

Pairing the Buttons with the Zigbee2MQTT coordinator was simple as usual, but I had some trouble to figure out how to use it in Home Assistant. Zigbee2MQTT recommends to use the MQTT device triggers but the documentation wasn't that clear on this part.

The first thing to do is to figure out the available triggers of the device. Triggers are published to the MQTT topic <discovery_prefix>/device_automation/ and you can subscribe to is to discover the messages. To subscribe to an MQTT topic mosquitto_sub CLI command can be used e.g. mosquitto_sub -h 192.168.0.1 -p 8883 -u user -P secure-password -t homeassistant/device_automation/# or the Home Assistant UI at Settings / Devices & Services / Integrations / MQTT / Configure / Listen to a topic.

After subscribing to the topic, remember that device triggers are not published until the event has been triggered at least once on the device. Afterwards the following triggers should be listed for the Tradfri Button:

{
    "automation_type": "trigger",
    "device": {
        "identifiers": [
            "zigbee2mqtt_0x84b112233445566"
        ],
        "manufacturer": "IKEA",
        "model": "TRADFRI shortcut button (E1812)",
        "name": "livingroom/switch_test",
        "sw_version": "2.3.080"
    },
    "payload": "off",
    "subtype": "off",
    "topic": "zigbee2mqtt/livingroom/switch_test/action",
    "type": "action"
}
{
    "automation_type": "trigger",
    "device": {
        "identifiers": [
            "zigbee2mqtt_0x84b112233445566"
        ],
        "manufacturer": "IKEA",
        "model": "TRADFRI shortcut button (E1812)",
        "name": "livingroom/switch_test",
        "sw_version": "2.3.080"
    },
    "payload": "brightness_stop",
    "subtype": "brightness_stop",
    "topic": "zigbee2mqtt/livingroom/switch_test/action",
    "type": "action"
}
{
    "automation_type": "trigger",
    "device": {
        "identifiers": [
            "zigbee2mqtt_0x84b112233445566"
        ],
        "manufacturer": "IKEA",
        "model": "TRADFRI shortcut button (E1812)",
        "name": "livingroom/switch_test",
        "sw_version": "2.3.080"
    },
    "payload": "brightness_move_up",
    "subtype": "brightness_move_up",
    "topic": "zigbee2mqtt/livingroom/switch_test/action",
    "type": "action"
}
{
    "automation_type": "trigger",
    "device": {
        "identifiers": [
            "zigbee2mqtt_0x84b112233445566"
        ],
        "manufacturer": "IKEA",
        "model": "TRADFRI shortcut button (E1812)",
        "name": "livingroom/switch_test",
        "sw_version": "2.3.080"
    },
    "payload": "on",
    "subtype": "on",
    "topic": "zigbee2mqtt/livingroom/switch_test/action",
    "type": "action"
}

Great, with this data determined, it's almost done. The only missing information is the device_id. I have not found a nice way to get this ID yet, but it is part of the URL after navigating to Settings / Devices & Services / Devices / <device>.

Finally an action can be assigned to a button. The example below is starting the Vacuum cleaner on a single_click (on) trigger:

- alias: Start Vaccum
  trigger:
    - platform: device
      domain: mqtt
      device_id: 61b011e2e7d5e111111d8d804a029f61
      type: action
      subtype: "on"
      discovery_id: 0x84b112233445566 action_on
  action:
    - service: vacuum.start
      target:
        entity_id: vacuum.valetudo_rockrobo