Can't get value from MQTT

In my configuration.yaml I have this entry :

sensor:
    - platform: mqtt
      state_topic: "tele/ZIGBEE/SENSOR"
      name: 'ButtonPower'
      value_template: '{{value_json.0x6E4E.Power}}'

When I listen to the topic tele/ZIGBEE/SENSOR in home assistant I get the message:

{

    "ZbReceived": {
        "0x6E4E": {
            "Device": "0x6E4E",
            "Name": "Bedroom_Switch",
            "0006!02": "",
            "Power": 2,
            "Endpoint": 1,
            "LinkQuality": 107
        }
    }
}

However on the Developer Tools page in home assistant under sensor.buttonpower it lists the value as unknown. I want the value to be 2, not unknown.

In the log I get this message:

2021-09-05 11:53:00 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: dict object has no element 28238 when rendering ‘{{value_json.0x6E4E.Power}}’

I’m running:
version core-2021.9.3
supervisor-2021.09.0

Do I need to define the power as an integer? What am I doing wrong?

Change the template to this:

      value_template: '{{value_json.ZbReceived["0x6E4E"].Power}}'

The original template overlooks to include ZbReceived in the JSON path. It also attempts to reference the 0x6E4E key using dot notation. There’s a problem with that when the key starts with a number instead of a letter. In that case, you should use bracket notation to reference the 0x6E4E key.

I think I’ve gotten a little bit further ahead. I changed my yaml to:
value_template: '{{value_json.ZbReceived.Power}}'

now in the Developer Tools page it changes from unknown to blank. It still doesn’t read the value, but it’s something.

YES! Thank-you so much!

1 Like