How to use {{ value_json.xxxxx.xxxxxx}} in configuration.yaml to get valid data

I have the following JSON coming from MQTT broker:

    {"end_device_ids": 
        {"device_id": "eui-70b3d57ed004d388",
        "application_ids": {"application_id": "mqtt-rpi3b"},
        "dev_eui": "70B3D57ED004D388",
        "join_eui": "0000000000000000",
        "dev_addr": "260C0875" },
    "received_at": "2022-02-27T23:45:18.013631675Z",
    "uplink_message": 
        {"session_key_id": "AX88sOZsNTkoN5rZ+Rzx/Q==",
          "f_port": 1,
          "f_cnt": 80,
          "frm_payload": "SGVsbG8sIHdvcmxkIQ==",
          "decoded_payload": 
                {"degreesC": 20.20263671875,
                "humidity": 38.1103515625
               }
        }
}

If I convert it to JavaSript with JSON node in Node-Red, and build a Function node to pull out the temp and humidity, it works perfectly, and I get the following in the Node-Red Debug window:

Home Assistant

2/28/2022, 9:59:23 AMnode: a99be1c0d3d3695dv3/mqtt-rpi3b@ttn/devices/eui-70b3d57ed004d338/up : msg.payload : array[1]
array[1]
0: object
Temp: 20.60546875
Humidity: 49.51171875

Now I don’t know how to set it up in configuration.yaml to get valid temp and humidity data. I tried

  - platform: mqtt
    state_topic: v3/mqtt-rpi3b@ttn/devices/eui-70b3d57ed004d338/#
    name: "Temp DS"
    value_template: '{{ value_json.uplink_message.decoded_payload.degreesC }}'
    unit_of_measurement: "°C"
  - platform: mqtt
    state_topic: v3/mqtt mqtt-rpi3b@ttn/devices/eui-70b3d57ed004d338/#
    name: "Hum_DS"
    value_template: '{{ value_json.uplink_message.decoded_payload.humidity }}'
    unit_of_measurement: "%"  

But all I get is “unknown” data. What might I be doing wrong?

What do your logs say? Also, for future reference, if you format the code, you can easily see the path you need to take. The way you have it written is difficult to follow.

{
   "end_device_ids":{
      "device_id":"eui-70b3d57ed004d388",
      "application_ids":{
         "application_id":"mqtt-rpi3b"
      },
      "dev_eui":"70B3D57ED004D388",
      "join_eui":"0000000000000000",
      "dev_addr":"260C0875"
   },
   "received_at":"2022-02-27T23:45:18.013631675Z",
   "uplink_message":{
      "session_key_id":"AX88sOZsNTkoN5rZ+Rzx/Q==",
      "f_port":1,
      "f_cnt":80,
      "frm_payload":"SGVsbG8sIHdvcmxkIQ==",
      "decoded_payload":{
         "degreesC":20.20263671875,
         "humidity":38.1103515625
      }
   }
}

Why not just create the entities from Node red instead using the entity node?

1 Like

Ya beat me to it…

1 Like

I’m assuming your json node returns the json object in the payload, that is what my inject does here.
So replace the inject with your MQTT and json node.

[{"id":"006fd1caf7bd8b1c","type":"inject","z":"ebaa69a9.649708","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"end_device_ids\":{\"device_id\":\"eui-70b3d57ed004d388\",\"application_ids\":{\"application_id\":\"mqtt-rpi3b\"},\"dev_eui\":\"70B3D57ED004D388\",\"join_eui\":\"0000000000000000\",\"dev_addr\":\"260C0875\"},\"received_at\":\"2022-02-27T23:45:18.013631675Z\",\"uplink_message\":{\"session_key_id\":\"AX88sOZsNTkoN5rZ+Rzx/Q==\",\"f_port\":1,\"f_cnt\":80,\"frm_payload\":\"SGVsbG8sIHdvcmxkIQ==\",\"decoded_payload\":{\"degreesC\":20.20263671875,\"humidity\":38.1103515625}}}","payloadType":"json","x":130,"y":600,"wires":[["6a38c371372ad1c1","43a49ebde556e7b1"]]},{"id":"6a38c371372ad1c1","type":"ha-entity","z":"ebaa69a9.649708","name":"Degrees","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload.uplink_message.decoded_payload.degreesC","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":320,"y":560,"wires":[[]]},{"id":"43a49ebde556e7b1","type":"ha-entity","z":"ebaa69a9.649708","name":"Humidity","server":"4bbca37b.1700ec","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":""},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload.uplink_message.decoded_payload.humidity","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":320,"y":640,"wires":[[]]},{"id":"4bbca37b.1700ec","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

image

1 Like

I could do that, but I’m just trying to avoid unnecessary applications and processing. Plus, I’m trying to learn how to do this.

Thanks for the format advice. The logs don’t show anything.

Thanks, Node-Red Entity nodes work, but still trying to avoid Node-Red unless there’s no alternative.

What’s the value of the topic v3/mqtt-rpi3b@ttn/devices/eui-70b3d57ed004d338/#?

It’s exactly what I showed at the top (that which you reformatted).

If that was the case, it should work out of the box with the configuration that you have. Otherwise you’d have errors in your logs.

What logs would I look at: Configuration/Settings/Logs? Everything seems to work, except the state is ‘unknown’. If I run it through Node-Red Entity node using the state below, it works fine.

msg.payload.uplink_message.decoded_payload.degreesC

Homeassistant.logs should contain errors or warnings regarding those sensors

Absolutely no related errors or warnings in /config/home-assistant.log or /config/home-assistant.log.1.

State still shows:

Not sure what to tell you. They should show up in some fashion. Maybe you’re limiting your logs? You can always try to force an error with typo’s too. That should produce a template error at a minimum.

I tried forcing an error by changing path from ‘uplink_message’ to ‘downlink_message’. But nothing in logs except the following, which I don’t think is related:

2022-02-28 14:17:30 WARNING (Thread-13) [homeassistant.util.executor] Thread[SyncWorker_3] is still running at shutdown: File "/usr/local/lib/python3.9/threading.py", line 930, in _bootstrap
    self._bootstrap_inner()
  File "/usr/local/lib/python3.9/threading.py", line 973, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.9/threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 77, in _worker
    work_item.run()
  File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 52, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/bluetooth_tracker/device_tracker.py", line 127, in lookup_name
    return bluetooth.lookup_name(mac, timeout=5)  # type: ignore[no-any-return]
  File "/usr/local/lib/python3.9/site-packages/bluetooth/bluez.py", line 64, in lookup_name
    name = _bt.hci_read_remote_name (sock, address, timeoutms)

I guess I’ll set it aside for a while and just use Node-Red as suggested.

Something in your setup is wrong then, my guess is that it’s your state topic and home assistant is looking at the wrong topic, therefore the value_template is never resolved. You have a ton of special characters, you might need to wrap it in quotes and escape the characters that may cause problems.