Parsing MQTT Events in HA

Forum for questions and support relating to the 1.34.x releases only.
Post Reply
Bearded_Beef
Posts: 17
Joined: Thu Mar 11, 2021 5:51 pm

Parsing MQTT Events in HA

Post by Bearded_Beef » Sat Jun 12, 2021 2:49 am

I already have a thread in the Home Assistant forum but I thought it might be worth asking around here as well. I am having trouble parsing the label from events. I can parse name with no issues, which at least gets me around the 255 character limit, but for some reason everything that I have found online about how to parse the label out of an MQTT notification has not worked well for me.

I tried:

value_template: "{{ value_json.detection[0].label }}"

value_template: "{{ value_json.detection[0].label }}"
json_attributes_topic: "zoneminder/1"
json_attributes_template: "{{ value_json.detection[0] }}"

and it was telling me there is no value dictionary value for 0 for label. So I look at the full error message when I send MQTT without parsing and noticed that the dictionary seems to actually be using "labels" not "label", for example:

ERROR (MainThread) [homeassistant.util.logging] Exception in message_received when handling msg on ‘zoneminder/2’: ‘{“eventtype”:“event_start”,“hookvalue”:“0”,“state”:“alarm”,“eventid”:“500”,“monitor”:“2”,“detection”:{“image_dimensions”:{“original”:[1944,2592],“resized”:[600,800]},“confidences”:[0.985492706298828,0.94365668296814],“frame_id”:“snapshot”,“boxes”:[[138,248,216,468],[504,38,546,168]],“labels”:[“person”,“person”]},“name”:“Driveway:(500) [s] detected:person:99% Linked”}’

Someone told me to try this on the HA forums:

value_template: {{ value_json.detection.labels }}

But I started getting alternating dictionary and list errors. Does anyone else have a similar setup working or have any ideas? My Google Fu is failing me.

Bearded_Beef
Posts: 17
Joined: Thu Mar 11, 2021 5:51 pm

Re: Parsing MQTT Events in HA

Post by Bearded_Beef » Sun Jun 13, 2021 4:19 am

JIC anyone else goes looking, label has indeed been changed to labels and has itself been turned into a list. Heres what I did to get a working MQTT Sensor for ZM events. You can add the image as well but I'm sending those via smtp and just using this for notifications for things like alarm panels, SMS and what not.

- platform: mqtt
state_topic: "zoneminder/1"
name: "driveway_alert"
value_template: "{{ value_json.detection.labels[0] }}"
force_update: true
expire_after: 120

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests