Fixed: template variable warning 'dict object' has no attribute 'action' when rendering '{{ value_json.action }}'

This might help someone in the future. I spent more time than I care to admit figuring it out; I hope it’s OK to post something like this here.

In my logs, I had this line repeated every 5 minutes:

(MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute 'action' when rendering '{{ value_json.action }}'

Watching the Z2M logs, it turns out that my Develco/Frient KEPZB-110 shows an MQTT payload every 5 minutes that contains no “action” parameter (since there was no button pressed), and the log warning is indicating that the action parameter was missing from the JSON payload.

Turns out I had an automation using the MQTT trigger with a payload defined as part of the trigger:

triggers:
  - alias: Keypad arm day zones (house) button pressed
    topic: zigbee2mqtt/Keypad-1
    value_template: "{{ value_json.action }}"
    trigger: mqtt
    payload: arm_day_zones

Every 5 minutes when the payload appeared without “action”, it generated the warning above. I’m not sure if it’s the keypad checking in every 5 minutes or something else polling it. I don’t think I have any automations with a 5 minute run schedule relating to this.

I fixed it by listening for the MQTT topic without a payload and using a condition to check if “action” is defined and matches what I’m looking for.

triggers:
  - alias: Keypad arm day zones (house) button pressed
    topic: zigbee2mqtt/Keypad-1
    value_template: "{{ value_json.action }}"
    trigger: mqtt
conditions:
  - condition: template
    value_template: " {{  trigger.payload_json['action'] is defined and trigger.payload_json['action'] == \"arm_day_zones\" }}"

If action isn’t defined it just exits with no error, which is what I want. In my searching I came across a handful of log warnings like this with different solutions but I didn’t see a case like this listed, so I figured it was worth writing down in case it helps someone else.