Reading additional values from MQTT

I receive a MQTT response as follows and I want to read the BAT_V value in HA. Thanks in advance.

MQTT message:

{
“app_eui”: “A84041000000”,
“dc”: {
“balance”: 8956,
“nonce”: 1
},
“decoded”: {
“payload”: {
“ALARM”: 0,
“BAT_V”: 3.006,
“DOOR_OPEN_STATUS”: 0,
“DOOR_OPEN_TIMES”: 53,
“LAST_DOOR_OPEN_DURATION”: 0,
“MOD”: 1
},
“status”: “success”
},

Helium Decoder:

if(mod==1){
var open_times=bytes[3]<<16 | bytes[4]<<8 | bytes[5];
var open_duration=bytes[6]<<16 | bytes[7]<<8 | bytes[8];//units:min
if(bytes.length==10 && 0x07>bytes[0]< 0x0f)
return {
BAT_V:bat,
MOD:mod,
DOOR_OPEN_STATUS:door_open_status,
DOOR_OPEN_TIMES:open_times,
LAST_DOOR_OPEN_DURATION:open_duration,
ALARM:alarm
};

My YAML looks like this and the Mailbox Door part works. How do I enter the YAML to read the BAT_V value?

mqtt:
binary_sensor:
- name: “Mailbox Door”
device_class: opening
payload_on: “1”
payload_off: “0”
payload_not_available: “”
value_template: “{{ value_json.decoded.payload.DOOR_OPEN_STATUS }}”
state_topic: “helium/a11faa84-b9d6-4fc3-a173-4acd35bec9ee/rx”

Welcome!

Have a look at the ‘json_attributes_topic:’ .
This will suck in the full MQTT message, you can the parse out specific attributes at any time. This is perhaps more info than you want, however if the update frequency is not too great, it is kind of nice to have all of the info recorded in history for a sensor.

Or you can create just a Battery sensor in addition to your binary_sensor, by using the ‘sensor’ construct. If you include the ‘json_attributes_topic:’, just do it for one of your sensors not multiple, as this is redundent.

Good hunting!

    json_attributes_topic: "zigbee2mqtt/mailbox-sensor-baaf"

Please format your code properly. I’ve had to fix indentation and quotes in my code below which is modified from yours.

This goes under MQTT also, with the same indentation as your binary sensor:

sensor:
  - name: "Mailbox battery voltage"
    device_class: voltage
    unit_of_measurement: 'V'
    value_template: "{{ value_json['decoded']['payload']['BAT_V'] }}"
    state_topic: "helium/a11faa84-b9d6-4fc3-a173-4acd35bec9ee/rx"

I’ve used bracket notation rather than dot notation in the JSON path, which is always better as it works with attribute names that start with digits and with reserved keywords like values.

I’m going to experiment with this. Thank you very much.

Awesome. I’ll see what this gives me. Thank you.

Unfortunately that doesn’t work! It looked promising.
sensor:

  • name: “Mailbox battery voltage”
    device_class: voltage
    unit_of_measurement: ‘V’
    value_template: “{{ value_json[‘decoded’][‘payload’][‘BAT_V’] }}”
    state_topic: “helium/a11faa84-b9d6-4fc3-a173-4acd35bec9ee/rx”

image

Anything in the logs? Please format your code — there could be errors we simply can’t see with it pasted in like that.

I appreciate the help.

Nothing in the logs when I use:

sensor:

  • name: “Mailbox Battery”
    device_class: voltage
    unit_of_measurement: ‘V’
    value_template: “{{ value_json[‘decoded’][‘payload’][‘BAT_V’] }}”
    state_topic: “helium/a11faa84-b9d6-4fc3-a173-4acd35bec9ee/rx”

I tried both versions in Template editor and they return the correct value of 3. Now if I could just get that on to the card!

These work in Template editor:

{% set value_json = {“app_eui”:“A840410000000107”,“dc”:{“balance”:8898,“nonce”:1},“decoded”:{“payload”:{“ALARM”:0,“BAT_V”:3,“DOOR_OPEN_STATUS”:0,“DOOR_OPEN_TIMES”:89,“LAST_DOOR_OPEN_DURATION”:0,“MOD”:1},“status”:“success”},“dev_eui”:“A8404125F186DAFF”,“devaddr”:“16000048”,“fcnt”:475,“hotspots”:[{“channel”:2,“frequency”:904.3,“hold_time”:0,“id”:“11RZouCw8oNJ6yzksTD1iF4QsE9i6vRKxUN6ttb4WdyYvap4BDw”,“lat”:39.0949483896952,“long”:-86.5624449301574,“name”:“custom-ocean-lemur”,“reported_at”:1711123444306,“rssi”:-131.0,“snr”:-15.0,“spreading”:“SF10BW125”,“status”:“success”}],“id”:“a11faa84-b9d6-4fc3-a173-4acd35bec9ee”,“metadata”:{“adr_allowed”:false,“cf_list_enabled”:false,“multi_buy”:1,“organization_id”:“848f817e-5796-44e6-ba50-ba54b0124071”,“preferred_hotspots”:,“rx_delay”:1,“rx_delay_actual”:1,“rx_delay_state”:“rx_delay_established”},“name”:“Mailbox”,“payload”:“C7gBAABZAAAAAA==”,“payload_size”:10,“port”:10,“raw_packet”:“QBYAAEiA2wEKdVdQR1t8jCAupeWyo2c=”,“replay”:false,“reported_at”:1711123444306,“type”:“uplink”,“uuid”:“fc145675-8145-4f28-91aa-26857f431730”} %}

{{ value_json.decoded.payload.BAT_V }}
{{ value_json[‘decoded’][‘payload’][‘BAT_V’] }}

How do I format my code it keeps changing the hyphen to a dot!

f6be36681e0da431418ec7781fb6c62712941803

See the link I posted above. Either use the </> button or surround the code with three backticks:

```
Code goes here
```

Thank you! I appreciate it.

This is what I have now and I get ‘Unavailable’ for Mailbox Battery

sensor:
  - name: "Mailbox battery voltage"
    device_class: voltage
    unit_of_measurement: 'V'
    value_template: "{{ value_json['decoded']['payload']['BAT_V'] }}"
    state_topic: "helium/a11faa84-b9d6-4fc3-a173-4acd35bec9ee/rx"

Thanks again folks.

indentation:

mqtt:
  binary_sensor:
    - name: “Mailbox Door”
      device_class: opening
      payload_on: “1”
      payload_off: “0”
      payload_not_available: “”
      value_template: “{{ value_json.decoded.payload.DOOR_OPEN_STATUS }}”
      state_topic: “helium/a11faa84-b9d6-4fc3-a173-4acd35bec9ee/rx”

  sensor:
    - name: "Mailbox battery voltage"
      device_class: voltage
      unit_of_measurement: 'V'
      value_template: "{{ value_json['decoded']['payload']['BAT_V'] }}"
      state_topic: "helium/a11faa84-b9d6-4fc3-a173-4acd35bec9ee/rx"

(binary sensor is wrong,wrong ", because I formatted your openingspost, but the " are already mangled.

Did you adjust the title on the card? The sensor should be called sensor.mailbox_battery_voltage. If you haven’t explicitly set the name on the card, you might have a non-existent sensor selected.

YEAH! That was it. I picked up the wrong name for the card. The old name of binary_sensor.mailbox_battery was hanging around and I used it versus sensor.mailbox_battery.

So the working version is:

mqtt:
  binary_sensor:
    - name: "Mailbox Door"
      device_class: opening
      payload_on: "1"
      payload_off: "0"
      payload_not_available: ""
      value_template: "{{ value_json['decoded']['payload']['DOOR_OPEN_STATUS'] }}"
      state_topic: "helium/a11faa84-b9d6-4fc3-a173-4acd35bec9ee/rx"
  sensor:
    - name: "Mailbox Battery"
      device_class: voltage
      unit_of_measurement: 'V'
      value_template: "{{ value_json['decoded']['payload']['BAT_V'] }}"
      state_topic: "helium/a11faa84-b9d6-4fc3-a173-4acd35bec9ee/rx"

I can’t thank you folks enough this was driving me crazy. I’m going to learn this stuff yet! Thanks again.

That was it. Thank you, thank you!

1 Like