MQTT sensor attribute with json

I am trying to make a MQTT sensor with information from json.

I have this MQTT sensor:

mqtt:
  sensor:
    - name: "Scrypted Kitchen sensor"
      state_topic: "scrypted/kitchen-camera/motionDetected"
      json_attributes_topic: "scrypted/kitchen-camera/ObjectDetector"
      payload_on: "true"
      payload_off: "false"
      device_class: motion

In MQTT Exlorer, I see this:

motionDetected = true
ObjectDetector = {“detections”:[{“boundingBox”:[1048,728,498,350],“className”:“person”,“score”:0.7890625,“id”:“969995f9-d691-43bf-bf26-776b604c4cc6”,“history”:{“firstSeen”:1672323543627,“lastSeen”:1672323544236},“bestScore”:0.9921875}],“inputDimensions”:[1920,1080],“running”:true,“timestamp”:1672323544234}

How do I make a sensor with state from “className”? It works for me to make a sensor with state from ‘motionDetected’ and with ‘ObjectDetector’ as attribute:

:thinking:

     ....
    device_class: motion
    json_attributes:
      - className

Invalid config for [mqtt]: [json_attributes] is an invalid option for [mqtt]

:frowning:

it’s not in the mqtt sensor section. i.e. bad spacing

Updated first post to show, that the code is in the mqtt sensor section.
And can via Google see, that json_attributes have been removed.

but that error is telling you’re going straight from mqtt to json_attributes.

e.g.

mqtt:
  json_attributes:

instead of

mqtt:
  sensor:
  - ...
    json_attributes:

As to the documentation

json_attributes is not a variable.

Then you’d just need the json_attributes_template: "{{ value }}"

I will try to explain a bit more …

It is possible for me to make a “Timestamp” sensor with the number as value. But what I want is to take a value from within “Detections” (see screenshot).

mqtt:
  sensor:
    - name: "Scrypted Living Room sensor timestamp"
      state_topic: "scrypted/living_room-camera/ObjectDetector"
      value_template: "{{ value_json.timestamp }}"

How do I make the value from Detections, className as a sensor?

  value_template: "{{ (value_json.timestamp / 1000) | timestamp_local | as_datetime }}"

Thanks, made my timestamp look better.

But I want a subset of “Detections”, the value of className.

{"detections":[{"boundingBox":[987,702,544,376],"className":"person","score":0.93359375,"id":"627487bb-0170-4456-bcc7-afb139976ad5","history":{"firstSeen":1672343055486,"lastSeen":1672343055906},"bestScore":0.98046875}],"inputDimensions":[1920,1080],"running":true,"timestamp":1672343055905}

value_json.detections[0].className

That worked, thanks!

1 Like

@onkelfarmor could you share your complete sensor definition?
I am struggling with the exact same problem: getting Scrypted cameras values in a HA sensor.

For simplicity I am limiting the objects detected to the first of the array and setting up separate sensors per object types (person, cat ,dog)

TX in advance

So frustrated that I think next step will be writing a short appdaemon that will solve that simple and easy.

1 Like

Here you are:

mqtt:
  sensor:
    - name: "Scrypted Living Room sensor timestamp"
      state_topic: "scrypted/living_room-camera/ObjectDetector"
      value_template: "{{ (value_json.timestamp / 1000) | timestamp_local | as_datetime }}"
      unique_id: livingroomcametimestamp
      device_class: timestamp
      device:
        name: Scrypted Living Room
        identifiers:
          - '1002'  

    - name: "Scrypted Living Room ClassName sensor"
      state_topic: "scrypted/living_room-camera/ObjectDetector"
      value_template: "{{ value_json.detections[0].className }}"
      unique_id: livingroomcamclassname
      device:
        name: Scrypted Living Room
        identifiers:
          - '1002'  
          
    - name: "Scrypted Living Room score"
      state_topic: "scrypted/living_room-camera/ObjectDetector"
      value_template: "{{ value_json.detections[0].score }}"
      unique_id: livingroomcamscore
      device_class: power_factor
      device:
        name: Scrypted Living Room
        identifiers:
          - '1002'  

    - name: "Scrypted Living Room id"
      state_topic: "scrypted/living_room-camera/ObjectDetector"
      value_template: "{{ value_json.detections[0].id }}"
      unique_id: livingroomcamid
      device:
        name: Scrypted Living Room
        identifiers:
          - '1002'  
          
    - name: "Scrypted Living Room bestscore"
      state_topic: "scrypted/living_room-camera/ObjectDetector"
      value_template: "{{ value_json.detections[0].bestScore }}"
      unique_id: livingroomcambestscore
      device:
        name: Scrypted Living Room
        identifiers:
          - '1002'  
2 Likes

Thank You Very much!