MQTT attribute based binary sensor: "MQTT - Dict Object has no attribute", a little help please for one who is YAML and Jinja2 challenged

I am trying to create a binary sensor based on the values in an attribute in a MQTT JSON string. I want the binary sensor to trigger if the attribute contains one of three possible values :

JSON attribute "action" has one of following values : ["tilt", "vibration", "drop"]

.

I think I have a basic version of this working, 2nd YAML code block below. I’m still not sure I have covered all the boundary cases, however for the three values I believe I am getting a successful trigger.

The boundary case I am asking for help here, is the case where the JSON attribute “action” is not published in one of the messages. This happens when the sensor publishes temperature, “I’m alive” or signal quality messages. Right now, when my binary sensor evaluates on a message that is missing this key, I get a warning in the HA log:
“MQTT - Dict Object has no attribute”

I’m trying the code in the first block of YAML below, however it is not working. I’m pointing at my lack of YAML and Jinja2 knowledge as source. Seems from some forum posts by some knowledgeable folks on this ‘missing attribute’ topic, you should be https://community.home-assistant.io/t/mqtt-dict-object-has-no-attribute/297330/40able to test for presence or absence using the ‘in value_json’ syntax.

I believe the first block of code should evaluate to either ‘true’ or ‘false’, however my binary sensor stays stuck at ‘unknown’.

Thanks in advance!

I’ve tried:

{% if action in value_json %}
# and
{% if "action" in value_json %}
# and
{% if 'action' in value_json %}

Example of the MQTT message, 1st with the attribute “action:” and 2nd without it are included below.

# mqtt binary sensors
binary_sensor:

# not working
# https://www.zigbee2mqtt.io/devices/DJT11LM.html#xiaomi-djt11lm
# https://svn.python.org/projects/external/Jinja-2.1.1/docs/_build/html/templates.html#other-operators
  - name: "Mailbox Vibration"
    device_class: vibration
    unique_id: "0x00158d0009ee3f7a-mailbox-vibration"
    expire_after: 3600
    state_topic: "zigbee2mqtt/0x00158d0009ee3f7a"
    value_template: >-
      {% if action in value_json %}
        {{ value_json.action | string | default("OFF", true) in ["tilt", "vibration", "drop"] }}
      {% else %}
        false
      {% endif %}
    payload_off: false
    payload_on: true
    availability_topic: zigbee2mqtt/0x00158d0009ee3f7a/availability
    json_attributes_topic: "zigbee2mqtt/0x00158d0009ee3f7a"

# working
# https://svn.python.org/projects/external/Jinja-2.1.1/docs/_build/html/templates.html#other-operators
  - name: "Mailbox Vibration"
    device_class: vibration
    unique_id: "0x00158d0009ee3f7a-mailbox-vibration"
    expire_after: 3600
    state_topic: "zigbee2mqtt/0x00158d0009ee3f7a"
    value_template: '{{ value_json.action | string | default("OFF", true) in ["tilt", "vibration", "drop"] }}'
    payload_off: false
    payload_on: true
    availability_topic: zigbee2mqtt/0x00158d0009ee3f7a/availability
    json_attributes_topic: "zigbee2mqtt/0x00158d0009ee3f7a"
{"action":"tilt","angle":107,"angle_x":-2,"angle_x_absolute":92,"angle_y":25,"angle_y_absolute":65,"angle_z":65,"battery":70,"device_temperature":14,"elapsed":2098,"last_seen":"2023-12-21T17:37:15-08:00","linkquality":65,"power_outage_count":15,"strength":2,"vibration":true,"voltage":2955,"unit_of_measurement":"°F","device_class":"temperature","friendly_name":"Mailbox Temperature"}
{"angle":107,"angle_x":3,"angle_x_absolute":87,"angle_y":-81,"angle_y_absolute":171,"angle_z":9,"battery":77,"device_temperature":17,"elapsed":300464,"last_seen":"2023-12-22T09:18:28-08:00","linkquality":65,"power_outage_count":15,"strength":5,"vibration":true,"voltage":2965,"unit_of_measurement":"°F","device_class":"temperature","friendly_name":"Mailbox Temperature"}

Never mind, all I had to do was write down my stupidity…

    value_template: >-
      {% if "action" in value_json %}
        {{ value_json.action | string | default("OFF", true) in ["tilt", "vibration", "drop"] }}
      {% else %}
        {{ false }}  #<-  idiot.... 💡🤡
      {% endif %}