YAML automations without device_id

I have the following Automation I built with the GUI.

alias: Ben Room Wake Up
description: ""
trigger:
  - device_id: 41d62e5f1d793db5214849ba0a1a6faa
    domain: zha
    platform: device
    type: remote_button_double_press
    subtype: button
condition: []
action:
  - type: turn_on
    device_id: a4659e3c5bb97053b6e86f0b98115608
    entity_id: light.uplighter_l
    domain: light
  - type: turn_on
    device_id: daf6c0d07ed5871c19a709ed2a820c91
    entity_id: light.uplighter_r
    domain: light
  - type: turn_on
    device_id: 9834ea6c60ed503a9e2d348f9c5f6980
    entity_id: switch.plug_ben_1_amp_screen_laptop
    domain: switch
mode: single

In the action section the device_id seems a bit superfluous and obscure but if I remove the device_id lines I get ‘Message malformed: Unable to determine action @ data[‘action’][0]’. Is there a better way of doing this if I was writing by hand, that is more human readable?

Also the trigger device is called ‘sensor_button_pressed’. Is there a way of refuring to it by name rather than device_id?

Use the light.turn_on and switch.turn_on services in the actions. They only require an entity id and have other advantages too. See: What happens if I have to replace a device - #2 by tom_l

You can use an event trigger instead of the device trigger. Automation Trigger - Home Assistant

2 Likes

Hi, I have the actions working but cant figure out the event trigger bit. I have

alias: Ben Room Wake Up
description: ""
trigger:
  - device_id: 41d62e5f1d793db5214849ba0a1a6faa
    domain: zha
    platform: device
    type: remote_button_double_press
    subtype: button
  - platform: event
    event_type: ""
condition: []
action:
  - service: light.turn_on
    target:
      entity_id:
        - light.ben_bedroom_uplighters
    data: {}
  - service: switch.turn_on
    target:
      entity_id: switch.plug_ben_1_amp_screen_laptop
    data: {}
mode: single

I added a second Trigger as an event trigger but cant figure it out (just kept it there for reference, will delete later). I looked at Automation Trigger - Home Assistant but cant figre out the event type. The GUI does not have a pick list and cant find a lit is docs.

 trigger:
    - platform: numeric_state
      entity_id: climate.kitchen
      attribute: current_temperature
      above: 23

Is the closest I can find but its not quite right. Ive set the entity to ‘button.ben_bed_button’. Looked in Developer Tools → States to find atributes but no joy. Also trying to figre out platform. Did a google search on ‘platform homeassistant button’ but did not find anything usefull. Also wondering (and did some googleing) about doble click with nothing usefull.

You can monitor events in Developer Tools > Events. A Zigbee Home Automation (ZHA) event contains data like this (I copied this example from someone else’s post):

{
    "event_type": "zha_event",
    "data": {
        "device_ieee": "84:ba:20:ff:fe:8d:5d:56",
        "unique_id": "84:ba:20:ff:fe:8d:5d:56:1:0x0006",
        "device_id": "fbbcb195f2d27d3608f41df5e981e411",
        "endpoint_id": 1,
        "cluster_id": 6,
        "command": "remote_button_double_press",
        "args": []
    },
    "origin": "LOCAL",
    "time_fired": "2022-02-23T09:13:06.627854+00:00",
    "context": {
        "id": "11235396fee326f56854a077a5358890",
        "parent_id": null,
        "user_id": null
    }
}

Based on that ZHA event example, we can create an Event Trigger like this:

alias: Ben Room Wake Up
description: ""
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: 41d62e5f1d793db5214849ba0a1a6faa
      command: remote_button_double_press
condition: []
action:
  - service: homeassistant.turn_on
    target:
      entity_id:
        - light.ben_bedroom_uplighters
        - switch.plug_ben_1_amp_screen_laptop
mode: single

NOTE

If you ever need the value of a device’s device_id you can get it by using one of the following methods:

  1. Go to Developer Tools > Template and use the following template. Replace light.example with any of the entities that belong to your device.
{{ device_id('light.example') }}
  1. Go to Settings > Devices & Services > Devices and click the desired device. The value of its device_id will be displayed in the web page’s URL. For example: homeassistant.local:8123/config/devices/device/32175889ed5ab277dDE78b935798f5d
1 Like

Thanks for that, all verry usefull. I took a leap of fathe and tried

alias: Ben Room Wake Up
description: ""
trigger:
  - platform: event
    event_type: remote_button_double_press
condition: []
action:
  - service: light.turn_on
    target:
      entity_id:
        - light.ben_bedroom_uplighters
    data: {}
  - service: switch.turn_on
    target:
      entity_id: switch.plug_ben_1_amp_screen_laptop
    data: {}
mode: single

And it worked. I tried adding event_id and trigger: event_id: but it was malformed. Alough it works I fear it is not targeting the specific button, currently I only have one but when I have several feels this will fire for them all? I was trying not to use device_id in trigger. Love the ‘service: homeassistant.turn_on’. So feels as bit conseptualy like *.turn_on.

Go to Developer Tools > Events and set it up to listen for ZHA events. Then double-press your device’s button. The generated ZHA event will be displayed and you’ll see exactly what you need to configure an Event Trigger.

You’re right; it’s not. It’s triggering for anything that produces a remote_button_double_press event_type.

1 Like

What does your device provide on sensors, binary_sensors or whatever? :slight_smile: Or just, what device is this remote?

Right now, we only know, that you get an event, if the button is pressed. Maybe the remote provides something like a sensor with a state. I can only speak for my remote (it’s an IKEA Tradfri), and there I get a state similar to the action performed on the button. So when I press once, I get a state of arrow_right_click in my sensor.tradfri_remote. With this I can set the trigger to a state trigger and can avoid all device thingys in my config.

Are you saying the Event Trigger that I posted in my example was incorrect?

Sorry, I think I am being dumb. I cant find any trigger examples you have given that do not use device_id (i.e. that use entity_id instead).

Rereading the post it seems the only way I can not specify the decive_id in the trigger

trigger:
  - device_id: 41d62e5f1d793db5214849ba0a1a6faa
    domain: zha
    platform: device
    type: remote_button_double_press
    subtype: button

Is to define a template and do something like

trigger:
  - device_id: {{ device_id('button.ben_bed_button') }}
    domain: zha
    platform: device
    type: remote_button_double_press
    subtype: button

And there is no way to rewrite this trigger to use

entity_id: button.ben_bed_button

Somewhate in the trigger.

What I was hoping is I could add entity_id to the trigger and do something like

trigger:
  - platform: event
    event_type: remote_button_double_press
    entity_id: button.ben_bed_button

So I guess I need to learn about templates. Or have I missed something.

I didn’t post that kind of example because, as you can see from the sample ZHA event I posted above, the ZHA event does not contain an entity_id key. Therefore it’s not possible to do what you hoped to do here:

Regarding the use of a template, be aware that a Device Trigger does not support templating. Therefore you cannot do this:

However, I believe an Event Trigger supports templating so you can try this:

alias: Ben Room Wake Up
description: ""
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: "{{ device_id('button.ben_bed_button') }}"
      command: remote_button_double_press
condition: []
action:
  - service: homeassistant.turn_on
    target:
      entity_id:
        - light.ben_bedroom_uplighters
        - switch.plug_ben_1_amp_screen_laptop
mode: single

Thanks for this. I Tried

trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: "{{ device_id('button.ben_bed_button') }}"
      command: remote_button_double_press

as you sugested and it did not work but neither did

trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: 41d62e5f1d793db5214849ba0a1a6faa
      command: remote_button_double_press

So wondering if something else is wrong.

For reference I just retested

trigger:
  - device_id: 41d62e5f1d793db5214849ba0a1a6faa
    domain: zha
    platform: device
    type: remote_button_double_press
    subtype: button

And it worked so the devide ID is correct.

I tested ’ {{ device_id(‘button.ben_bed_button’) }}’ on Develiper Tools → TEMPLATES and it is defently working.

Should I re doing a restart or reloading config?

Did you perform the test I suggested previously?

OK this is weard. I did 2 single clicks, a long click and a doble click. Everything worked as expected (I hade the original working trigger with device_id). But

single click (light off)

event_type: zha_event
data:
  device_ieee: 00:12:4b:00:29:19:85:1a
  unique_id: 00:12:4b:00:29:19:85:1a:1:0x0006
  device_id: 41d62e5f1d793db5214849ba0a1a6faa
  endpoint_id: 1
  cluster_id: 6
  command: "on"
  args: []
  params: {}
origin: LOCAL
time_fired: "2023-07-04T22:20:14.185261+00:00"
context:
  id: 01H4HFVVF99C070YGMXG4QEC58
  parent_id: null
  user_id: null

single click (lights on)

event_type: zha_event
data:
  device_ieee: 00:12:4b:00:29:19:85:1a
  unique_id: 00:12:4b:00:29:19:85:1a:1:0x0006
  device_id: 41d62e5f1d793db5214849ba0a1a6faa
  endpoint_id: 1
  cluster_id: 6
  command: "off"
  args: []
  params: {}
origin: LOCAL
time_fired: "2023-07-04T22:20:03.253242+00:00"
context:
  id: 01H4HFVGSNPP59Q6CMBN3QJQQG
  parent_id: null
  user_id: null

long click (ligths and powe off)

event_type: zha_event
data:
  device_ieee: 00:12:4b:00:29:19:85:1a
  unique_id: 00:12:4b:00:29:19:85:1a:1:0x0006
  device_id: 41d62e5f1d793db5214849ba0a1a6faa
  endpoint_id: 1
  cluster_id: 6
  command: toggle
  args: []
  params: {}
origin: LOCAL
time_fired: "2023-07-04T22:19:53.799886+00:00"
context:
  id: 01H4HFV7J72HPTRPF42AAPQAFJ
  parent_id: null
  user_id: null

doble click (ligths and power on) - one one we are looking at.

event_type: zha_event
data:
  device_ieee: 00:12:4b:00:29:19:85:1a
  unique_id: 00:12:4b:00:29:19:85:1a:1:0x0006
  device_id: 41d62e5f1d793db5214849ba0a1a6faa
  endpoint_id: 1
  cluster_id: 6
  command: toggle
  args: []
  params: {}
origin: LOCAL
time_fired: "2023-07-04T22:19:49.177812+00:00"
context:
  id: 01H4HFV31S2TDY4F8EV52JVFQX
  parent_id: null
  user_id: null

So the command for long and doule click is both toggle, but how does it konw the diference. As I said everything is working fine. If I do two doble clicks it does nothing the second time).

Based on those results, the value for command should be toggle.

alias: Ben Room Wake Up
description: ""
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: "{{ device_id('button.ben_bed_button') }}"
      command: toggle
condition: []
action:
  - service: homeassistant.turn_on
    target:
      entity_id:
        - light.ben_bedroom_uplighters
        - switch.plug_ben_1_amp_screen_laptop
mode: single

I am afraid it does not work. Thanks for your help. I am going to have to leave it for now. Also this thread is geting quite long so seems best if I start another one focusing on the specific trigger question with a more targeted subject so others can more easily find it.

PS I actualy thought that may be the trigger, just could not figure how it diferentiated between doble tap and long press.

This topic’s title already describes the goal adequately for anyone searching how to specify something other than a device_id. Plus the example involves detecting a ZHA event which will be of interest to the many users of the ZHA integration.

Let’s isolate what does/doesn’t work. Here are four simple automations designed to report button events via a persistent notification. Try all four and see what does/doesn’t work.

alias: Button Event Version 1
description: "Detect all button events for device"
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: 41d62e5f1d793db5214849ba0a1a6faa
condition: []
action:
  - service: notify.persistent_notification
    data:
      title: Button Event Version 1
      message: "{{ trigger.event.data.command }}"
mode: single
alias: Button Event Version 2
description: "Detect toggle event for device"
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: 41d62e5f1d793db5214849ba0a1a6faa
condition:
  - condition: template
    value_template: "{{ trigger.event.data.command == 'toggle' }}"
action:
  - service: notify.persistent_notification
    data:
      title: Button Event Version 2
      message: "Detected Toggle Command"
mode: single
alias: Button Event Version 3
description: "Detect all button events for entity_id"
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: "{{ device_id('button.ben_bed_button') }}"
condition: []
action:
  - service: notify.persistent_notification
    data:
      title: Button Event Version 3
      message: "{{ trigger.event.data.command }}"
mode: single
alias: Button Event Version 4
description: "Detect toggle event for entity_id"
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: "{{ device_id('button.ben_bed_button') }}"
condition:
  - condition: template
    value_template: "{{ trigger.event.data.command == 'toggle' }}"
action:
  - service: notify.persistent_notification
    data:
      title: Button Event Version 4
      message: "Detected Toggle Command"
mode: single

You’ll notice that versions 2 and 4 use a Template Condition to detect the command type. Based on examples found elsewhere, this is a fairly common way to do it (as opposed to employing a command option in the Event Trigger).

Thanks, will give this a try but moving house this weekend so lots to do. Luckely everything is using a router I am going to take with me and use exclusivly for home automation.

Thanks a lot for all your help. Will get back to this next week.

In the meantime have a coffie.

1 Like

Thank you for the coffee and I hope your move goes smoothly. Good luck!

I couldn’t get any of the options using device_id() in the trigger working… guessing it’s a limited templates issue.

Moving it to the Conditions block does work:

trigger:
  - platform: event
    event_type: zha_event
    event_data:
      command: toggle
condition:
  - "{{ trigger.event.data.device_id == device_id('sensor.button.ben_bed_button') }}"
1 Like