MQTT sensors template

Hi.
I have this MQTT row:
{"samples": [{"dio-0": "False", "dio-4": "True"}], "batt": 12, "sig": -45, "online": "False"}

I take battery value by this configuration:

`sensor:

  • platform: mqtt
    state_topic: “/zigbee/location/0-13-a2-0-40-e6-eb-ef”
    name: “batt”
    unit_of_measurement: “V”
    value_template: ‘{{ value_json.batt }}’`

it works
HOW I CAN GET BINARY SENSOR FROM THIS ROW?

`binary_sensor:

  • platform: mqtt
    state_topic: “/zigbee/location/0-13-a2-0-40-e6-eb-ef”
    name: “dio-4”
    qos: 0
    payload_on: “True”
    payload_off: “False”
    sensor_class: opening
    value_template: ‘{{ value_json.samples.0.dio-4 }}’`

This binary sensor does not works!!

1 - Is it possible extract and make all binary sensors from MQTT row by one note in yaml file?
2 - Is it possible make MQTT binary sensors with attributes : “battery”, and “signal strength”?

Instead of:

value_template: ‘{{ value_json.samples.0.dio-4 }}’

Try this:

value_template: ‘{{ value_json.samples[0].dio-4 }}’

For reference, here is an excerpt from Home Assistants documentation on retrieving JSON from MQTT:

JSONPath query	             JSON
----------------             ---------------------------------
somekey	                     { 'somekey': 100 }
somekey[0]	             { 'somekey': [100] }
somekey[0].value             { 'somekey': [ { value: 100 } ] }
1 Like

value_json.samples
return
[{'dio-4': 'True', 'dio-0': 'False'}]

value_json.samples[0]
return
{'dio-4': 'True', 'dio-0': 'False'}

value_json.samples[0].dio-4
return

`ERROR:homeassistant.core:BusHandler:Exception doing job
Traceback (most recent call last):
File “/usr/local/lib/python3.4/dist-packages/homeassistant/core.py”, line 852, in job_handler
func(*args)
File “/usr/local/lib/python3.4/dist-packages/homeassistant/components/mqtt/init.py”, line 171, in mqtt_topic_subscriber
event.data[ATTR_QOS])
File “/usr/local/lib/python3.4/dist-packages/homeassistant/components/sensor/mqtt.py”, line 62, in message_received
hass, value_template, payload)
File “/usr/local/lib/python3.4/dist-packages/homeassistant/helpers/template.py”, line 37, in render_with_possible_json_value
return render(hass, template, variables)
File “/usr/local/lib/python3.4/dist-packages/homeassistant/helpers/template.py”, line 63, in render
}).render(kwargs).strip()
File “/usr/local/lib/python3.4/dist-packages/jinja2/environment.py”, line 989, in render
return self.environment.handle_exception(exc_info, True)
File “/usr/local/lib/python3.4/dist-packages/jinja2/environment.py”, line 754, in handle_exception
reraise(exc_type, exc_value, tb)
File “/usr/local/lib/python3.4/dist-packages/jinja2/_compat.py”, line 37, in reraise
raise value.with_traceback(tb)
File “”, line 1, in top-level template code
TypeError: unsupported operand type(s) for -: ‘Undefined’ and ‘int’

`

It doesn’t like the “-” in your dio names, so it it is taking it as an operator… basically it is seeing it like you are doing math, subtracting 4 from dio.

Can you have the names without the “-” sign?

Such as dio0, dio1, dio4, etc?

If you can send it over as:

{“samples”: [{“dio0”: “False”, “dio4”: “True”}], “batt”: 12, “sig”: -45, “online”: “False”}

and reference it like:

value_template: ‘{{ value_json.samples[0].dio4 }}’

I’m not sure where you are getting the JSON data from, so I’m not sure if you have control over the names

1 Like

I did it.
works.
Thank you.

and what about JSON

{“Time”:“2017-02-05T14:22:35”, “DS18B20”:{“Temperature”:“5.3”}}

where I want to get Temperature, yours

value_template: ‘{{ value_json.DS18B20[0].Temperature }}’

is not working

UPDATE:
This one is obviously works :slight_smile:

value_template: ‘{{ value_json.DS18B20.Temperature }}’

Hi guys,
sorry for bringing up such an old topic but I have similar problem.

I have DIY geiger counter that uses mqtt to send its measurements in json format.
It looks like this:
{"date": 31/10/2020, "time": 21:06:32, "cpm" : 59, "uSv": 0.0745, "voltage": 4.43, "time_period": 2000, "cptp": 2}

I am using this config:

   - platform: mqtt
     state_topic: "geiger"
     name: geiger
     expire_after: 90
     value_template: "{{ value_json.cpm }}"

When I go to MQTT settings and subscribe to the topic “geiger/” from there - I can see the messages above.
However my HA keeps telling me that
[sensor.geiger] unavailable friendly_name: geiger

I feel myself so stupid because it seems so elementary yet I can’t get it working!

Any ideas?!

Thanks

Well, I finally managed to get that working so, in case someone else is looking for the same, here is what I did.
Basically I did two steps and now sure which one helped - either this or that or both … or nothing :slight_smile:

  1. I send all data to homeassistant subtopic. Like for my geiger counter that goes to /homeassistant/geiger
  2. I put “” around values as well so here it is how it looks like:
{
  "date": "29/11/2020",
  "time": "20:55:33",
  "cpm": "69",
  "uSv": "0.09",
  "voltage": "4.58",
  "time_period": "60000",
  "cptp": "69"
}

and the corresponding config:

  - platform: mqtt
    state_topic: "homeassistant/geiger"
    name: geiger
    expire_after: 90
    value_template: "{{ value_json.uSv }}"
    unit_of_measurement: uSv

Hope that may help someone.