Difficulty in using triggers in a template

I’m currently trying to set up a sensor that updates based off zha events. the event is:

event_type: zha_event
data:
  device_ieee: 00:15:8d:00:09:13:aa:f4
  unique_id: 00:15:8d:00:09:13:aa:f4:1:0x0500
  device_id: bd6f6d7fd797118cc91ffe5fa4180805
  endpoint_id: 1
  cluster_id: 1280
  command: Tilt
  args:
    degrees: 102
  params: {}
origin: LOCAL
time_fired: "2023-11-07T20:20:48.432160+00:00"
context:
  id: 01HENQ3QNGZJW6MP067C460BBN
  parent_id: null
  user_id: nul

and the template sensor is:

template:
  - trigger:
      - platform: event
          event_type: "zha_event"
          event_data:
            device_id: bd6f6d7fd797118cc91ffe5fa4180805
            command: "Tilt"
    sensor:
      - name: "Garage Door Tilt"
        state: "{{ trigger.event.data.args.degrees }}"
        unique_id: garage_door_tilt
        attributes:
          time_fired: "{{ trigger.event.time_fired }}"

If I am understanding everything correctly this should make a sensor that updates the state whenever there is a ZHA event that alters the degrees of the ZHA object. Unfortunately I am getting a ‘trigger’ is undefined error message.

I think your indentation is off and no quotation marks around the event type:

template:
  - trigger:
      - platform: event
        event_type: zha_event
        event_data:
          device_id: bd6f6d7fd797118cc91ffe5fa4180805
          command: "Tilt"
    sensor:
      - name: "Garage Door Tilt"
        state: "{{ trigger.event.data.args.degrees }}"
        unique_id: garage_door_tilt
        attributes:
          time_fired: "{{ trigger.event.time_fired }}"

Unfortunately that doesn’t seem to change anything.

I’ve noticed if I change this line state: "{{ trigger.event.data.args.degrees }}" the leading argument is what is said as undefined.

Meaning if it is changed to state: "{{ example.event.data.args.degrees }}" the error message I get is ‘example’ is undefined.

You should have quotes around the event type like you had originally, but the indenting needed to be fixed like finity said.

This looks like the Aqara tilt sensor; I have config you can reference here.

Edit: I stand corrected. It looks like it doesn’t matter whether or not you use quotes around the event_type. Which leaves me wondering why your template isn’t working. What else does the log say? And are you certain you aren’t using that unique_id (garage_door_tilt) elsewhere in your config? The error you mentioned means that the trigger variable isn’t populated with data, which doesn’t seem possible if the only trigger for this template is what you’ve shown.

1 Like

Only required if the string has colons (:) in it.

Where are you getting that message? It kind of sounds like you are trying to test the sensor in the Template editor, which will not work for trigger-based values.

1 Like

Welp. You are correct I was using template editor and after just adding it into my configurations.yaml everything is showing and working properly.

Thank you all for the help.

1 Like