Use MQTT topic as part of template in automation

I’ve tried and have spent enough time trying similar things people have done to no avail for what I am trying to do. I use Double Take for facial recognition and it uses MQTT to communicate with HA and what I want to do is create an automation that TTS plays the name of the person who has been identified over some speakers. So far, I can get it going for specific person, but I don’t want to have to add people over and over. I wanted to use templating to have it insert person’s name based off of the data that is in MQTT.

It is in two places. A topic named after person is one of them, but I couldn’t figure out how to use that or create a sensor based off the name of a topic, only data inside of it. It’s also in a topic named after the camera that spotted them. With multiple cameras, I preferred the idea of using the topic that is created with their name, but if I can at least get it down to a particular camera, better than where I am now. Inside the camera topic, here is the data below. Same kind of data is in person’s name topic, but if I had to use the data inside the topic instead of the topic itself, then it my as well be in the camera topic that I get it from. I mostly was getting “UndefinedError: ‘value_json’ is undefined” using the template testing part of HA. Sensor itself had nothing in that I created.

{"id":"1672415020.311982-ad41ty","duration":0.35,"timestamp":"2022-12-30T15:43:40.991Z","attempts":2,"camera":"camera_name","zones":[],"matches":[{"name":"john doe","confidence":99.3,"match":true,"box":{"top":69,"left":351,"width":196,"height":246},"type":"snapshot","duration":0.3,"detector":"compreface","filename":"image.jpg","base64":null}],"misses":[],"unknowns":[],"personCount":1,"counts":{"person":1,"match":1,"miss":0,"unknown":0}}
`

You mentioned the word topic several times but overlooked to post an example of one. We’ll need to see one to help you extract anything from it.

What you posted above is a payload that was published to a topic. So do you want to extract information from a payload or a topic?

double-take/matches/john-doe

That is a topic example of a person when they have been identified. All other identified people would have a topic created inside of the topic “double-take/matches/”. I would prefer to use the subtopic created inside of the “double-take/matches/” topic as part of the template, that way when I have automation use TTS to read their name, I could type in some code that uses their name as what to say, which would be the “john-doe” subtopic for example, anything inside the “double-take/matches/” topic.

double-take/cameras/example_camera is another topic that has the name inside of the payload, but preferably I would do the first way as it is applicable to any camera.

Copy-paste the following into the Template Editor and experiment with it:

{% set topic = 'double-take/matches/john-doe' %}

{{ topic.split('/')[-1] }}

Post the automation you’re using and I can show you how to adapt it to get the user’s name from the topic. I assume it employs an MQTT Trigger.

Thanks. The result just says what is after the “double-take/matches/”. I don’t know how to change any of that to make it says what is actually after the real MQTT topic that starts with “double-take/matches/”.

Current automation is this while testing with one particular name. The last part is there to name the camera the identified person was spotted in front of. I would need to adapt it so that it pulls the camera name from the right person. The “state” of that entity that Double Take creates is always the latest camera they were spotted in front of, which is why that works for that.

alias: Double Take Test
description: ""
trigger:
  - platform: mqtt
    topic: double-take/matches/john-doe
condition: []
action:
  - service: tts.google_translate_say
    data:
      entity_id: media_player.example_speaker
      message: >-
        Attention: John Doe detected on the {{
        states('sensor.double_take_john_doe') | replace("_", " ") |
        title }}
mode: single
alias: Double Take Test
description: ""
trigger:
  - platform: mqtt
    topic: double-take/matches/#
condition: []
action:
  - service: notify.persistent_notification
    data:
      message: "Source: {{ trigger.topic.split('/')[-1] }}"
mode: single

Reference: Automation Trigger Variables - MQTT

1 Like

Awesome! Thanks, figured out how to put that into TTS action. Last bit to this since I also need to adapt the camera name and since the camera name is in the payload of the trigger topic as you can see from the first post, it looks like I can pull that out. I just couldn’t figure out how to pull it out with any info I found on this.

1 Like

Cleaned up here, this is actually from a match, not the camera topic.

{
“id”: “1640454076.141368-4otyo3”,
“duration”: 0.42,
“timestamp”: “2022-12-30T18:31:16.765Z”,
“attempts”: 2,
“camera”: “camera_name”,
“zones”: [],
“match”: {
“name”: “john doe”,
“confidence”: 97.91,
“match”: true,
“box”: {
“top”: 94,
“left”: 465,
“width”: 103,
“height”: 132
},
“type”: “snapshot”,
“duration”: 0.35,
“detector”: “compreface”,
“filename”: “345awf7-d4c-44ae-884-7c49b812551.jpg”,
“base64”: null
}
}
QoS: 0 - Retain: false

What in that payload do you want to get?

Bolded in my last post. I will then remove _, but I know how to do that, at least somewhat, from one of the other posts I made.

In a template, use trigger.payload_json.camera to get the camera key’s value.

1 Like

Thanks, that did the trick.

1 Like