Failing to extract value from JSON via MQTT

Hi Everyone,

I am struggling to extract a value from a JSON Object and was wondering if someone could help me out.

The message is actually arriving correctly at the MQTT Server in my HA. It seems to be just about the interpretation of the data.

This is the JSON:

{
  "end_device_ids": {
    "device_id": "eui-bc97",
    "application_ids": {
      "application_id": "klax-1"
    },
    "dev_eui": "BC9740",
    "join_eui": "70B3D5",
    "dev_addr": "260B7"
  },
  "correlation_ids": [
    "gs:uplink:01HPRZG71HTSG"
  ],
  "received_at": "2024-02-16T12:50:44.094863791Z",
  "uplink_message": {
    "session_key_id": "AY1o2VXaOfg8o",
    "f_port": 3,
    "f_cnt": 344,
    "frm_payload": "BEVVEQMJAUVTWREDm8SFAREPS38Qq0t/EE1Lfw/gS38PdA==",
    "decoded_payload": {
      "header": {
        "batteryPerc": 100,
        "configured": true,
        "connTest": false,
        "deviceType": "SML Klax",
        "meterType": "SML",
        "version": 1
      },
      "msgInfo": {
        "msgCnt": 1,
        "msgIdx": 85,
        "msgNum": 1
      },
      "payloads": [
        {
          "id": "090145535911039bc485",
          "type": "serverID"
        },
        {
          "register": {
            "filterActive": true,
            "filterId": 0,
            "unit": "Wh",
            "values": [
              {
                "valid": true,
                "value": 16715947
              },
              {
                "valid": true,
                "value": 16715853
              },
              {
                "valid": true,
                "value": 16715744
              },
              {
                "valid": true,
                "value": 16715636
              }
            ]
          },
          "type": "filter"
        }
      ],
      "type": "app"
    },
    "rx_metadata": [
      {
        "gateway_ids": {
          "gateway_id": "eui-58a0",
          "eui": "58A0CB"
        },
        "time": "2024-02-16T12:50:43.775126934Z",
        "timestamp": 4243676867,
        "rssi": -91,
        "channel_rssi": -91,
        "snr": 9.5,
        "location": {
          "latitude": 50.950,
          "longitude": 11.0,
          "source": "SOURCE_REGISTRY"
        },
        "uplink_token": "CiIKIAoUZXVpLTU4YTBjYmZmZmU4MDU0NTISCFigy//+gFRSEMO9xecPGgwIo7S9r",
        "received_at": "2024-02-16T12:50:43.863979522Z"
      }
    ],
    "settings": {
      "data_rate": {
        "lora": {
          "bandwidth": 125000,
          "spreading_factor": 7,
          "coding_rate": "4/5"
        }
      },
      "frequency": "868300000",
      "timestamp": 4243676867,
      "time": "2024-02-16T12:50:43.775126934Z"
    },
    "received_at": "2024-02-16T12:50:43.890499663Z",
    "consumed_airtime": "0.092416s",
    "version_ids": {
      "brand_id": "alpha-omega-technology",
      "model_id": "klax",
      "hardware_version": "2.0",
      "firmware_version": "2.0",
      "band_id": "EU_863_870"
    },
    "network_ids": {
      "net_id": "000013",
      "ns_id": "EC656E0000000181",
      "tenant_id": "ttn",
      "cluster_id": "eu1",
      "cluster_address": "eu1.cloud.thethings.network"
    }
  }
}

and this is my config

mqtt:
  sensor:
    - name: "Stromverbrauch"
      state_topic: "ttn/v3/klax-1@ttn/devices/eui-bc9740d2c7/up"
      value_template: "{{ value_json.uplink_message.decoded_payload.payloads[1].register.values[0].value }}"
      unit_of_measurement: "Wh"
      unique_id: klax_keller
      device:
        identifiers: "klaxhome"
        manufacturer: "AO-T"
        name: "Stromzähler"

This approach has already worked a couple of times but this is the first time I am using square brackets to choose object within a JSON. And somehow this doesn’t work. I don’t see a value beeing registered in the MQTT Entity.

Really appreciate any help I can get.

If I remember correctly values is a keyword and you will need to use square bracket notation to stop it being interpreted as such.

value_template: "{{ value_json.uplink_message.decoded_payload.payloads[1].register['values'][0].value }}"
1 Like

I will definately have to read up on keywords but this is working now. Thank you very much!

Use brackets throughout as a habit and you’ll never get caught out like this:

value_template: "{{ value_json['uplink_message']['decoded_payload']['payloads'][1]['register']['values'][0]['value'] }}"

3 Likes

Quick follow up: How do I divide this value by 1000? It seems logical enough but this doesn’t work:

value_template: "{{ value_json['uplink_message']['decoded_payload']['payloads'][1]['register']['values'][0]['value'] | / 1000 }}"

Remove the |.