Parsing MQTT Events in HA

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

Parsing MQTT Events in HA

Post by Bearded_Beef »

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: 18
Joined: Thu Mar 11, 2021 5:51 pm

Re: Parsing MQTT Events in HA

Post by Bearded_Beef »

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
strudenilist
Posts: 3
Joined: Wed Aug 10, 2022 12:01 pm

Re: Parsing MQTT Events in HA

Post by strudenilist »

The MQTT service I cooperate with is called Mosquitto.
They have a forum where you can get a better answer about MQTT HA.
This service helps keep the system more secure and stabilize the connection among different nods.
Usually, it works without flaws, and I am fully satisfied with its performance.
Thank you, Bearded_Beef, for your input and for sharing the code. I will try to implement this if I face any issues with this platform.
Can you suggest to me some more sources of information about this protocol? I want to get to know more about it.
plainequally
Posts: 3
Joined: Thu Dec 08, 2022 7:25 am

Re: Parsing MQTT Events in HA

Post by plainequally »

This service strengthens and stabilizes the connections between the various nodes in the system. I'd want to know more about it. run 3
Post Reply