MQTT Event configuration to translate mqtt payloads into events

OK. I think I need some help.

I have the Lutron Aurora dimmer switch, a zigbee device via z2m, and watching MQTT payloads, it does 2 types of payloads:
Every few minutes, the availability payloads would be like this:

{
    "battery": 100,
    "last_seen": "2025-06-06T16:02:12.612Z",
    "linkquality": 21
}

And if you click or turn the switch, the payload would be like this:

{
    "battery": 100,
    "last_seen": "2025-06-06T16:52:53.981Z",
    "linkquality": 40,
    "action": "brightness_move_to_level",
    "action_group": 25088,
    "action_level": 0,
    "action_transition_time": 0.07,
    "brightness": null,
    "update": {
        "installed_version": -1,
        "latest_version": -1,
        "state": null
    }
}

So what I can tell is that, if you click the button, you will get these 2 in the payload:

    "action": "brightness_move_to_level",
    "action_transition_time": 0.07,

and if you turn the rotary ring, you will get:

    "action": "brightness_move_to_level",
    "action_transition_time": 0.02,

(and there is only 0.07 and 0.02, whenever we have the brightness_move_to_level)

So, I figured I might be able to use MQTT Event with some templating to translate the behaviors above into 2 event types: press and turn

So here is what I have got so far. Not sure if there is any easier or shorter forms, or whether I’m missing anything.

mqtt:
  - event:
      name: "Aurora Switch Event"
      state_topic: "zigbee2mqtt/Aurora Dimmer Switch"
      value_template: |
        {% if value_json.action == 'brightness_move_to_level' and if value_json.action_transition_time == '0.07' %}
        "event_type": "press"
        {% else if value_json.action == 'brightness_move_to_level' and if value_json.action_transition_time == '0.02' %}
        "event_type": "turn"
        {% endif %}
      event_types:
        - press
        - turn

… and of course it does not work

… and I’m not sure what to do next, or how I go about to debug the thing.

Anything more info I need to provide…? Yes I have read the MQTT Event - Home Assistant document… but can’t say I understand the template part. I have read the Templating - Home Assistant and MQTT docs also.

A pointer would be appreciated.

Thank you Daryl.

Looks like I didn’t explain my intension well - not settled yet, but I plan to use the 2 different events not only for light control, but to trigger different types of automations.
Initially I tried to use MQTT triggers in automation, but then because the Aurora dimmer would emit 2 different styles of payloads, I would also get a bunch of warning messages in the log saying my action key in the MQTT trigger does not exist… when the payloads were about availability only.

Plus I’d like to use this opportunity to practice my template-fu… :slight_smile:

So… (I’m sure there are a bunch) where are the glaring errors to my attempt above…? I think (and hope) I am close, but stucked.

I don’t have experience with this, but based on the docs your value_template needs to return a dictionary but yours is returning a string.

Try this, where all I’ve done is added single curly brackets to the output:

mqtt:
  - event:
      name: "Aurora Switch Event"
      state_topic: "zigbee2mqtt/Aurora Dimmer Switch"
      value_template: |
        {% if value_json.action == 'brightness_move_to_level' and if value_json.action_transition_time == '0.07' %}
        { "event_type": "press" }
        {% else if value_json.action == 'brightness_move_to_level' and if value_json.action_transition_time == '0.02' %}
        { "event_type": "turn" }
        {% endif %}
      event_types:
        - press
        - turn

Edit:

Looked closer and your device is returning float types instead of strings, so the code above won’t work. You can just remove the quotes, but the other issue is that you don’t handle the case when the “action” key isn’t provided in the json (which occurs during the last_seen payload. You can handle this by using the get() method. Here’s what I’d recommend:

mqtt:
  - event:
      name: "Aurora Switch Event"
      state_topic: "zigbee2mqtt/Aurora Dimmer Switch"
      value_template: |
        {% set transition_time_mapping = {
          0.07: "press",
          0.02: "turn"
          } %}
        {% if value_json.get('action') == 'brightness_move_to_level'
          and value_json.get('action_transition_time') in transition_time_mapping.keys()  %}
          {{ { "event_type": transition_time_mapping[value_json.action_transition_time] } }}
        {% endif %}
      event_types:
        - press
        - turn

Thank you, Rick.

The first iteration of your codes as expected would give me errors in the log. Indeed I did not consider the scenario where the dimmer would give me the “short form” payloads where there is no action key. Upon reload HA would not even create the event entity.

So I followed your recommendations with the newer codes. This time there is indeed the event entity with 2 event types, however the it would not get triggered (no event) regardless what I do to the dimmer.
image
The strange part is that, nothing in the log either.

I guess I can play with single quote, double quote, etc. at different spots and see how they behave.
But is there other spot I should look into… to see what’s going on under the hoods? Or other things we can try differently…?