Mqtt sensor help

I was wondering if someone could help please…

I have configured a bridge and receiving topics… I am however a bit clueless when it comes to creating the MQTT sensor.

Here is the MQTT payload

{"applicationID":"10","applicationName":"WaterQuality","deviceName":"HT01","devEUI":"115cf59123ed86c7","rxInfo":[{"data":"haFDykFcAACicGjKQEzMzaJ1U80BkqJiVspAdwo9omIlPg==","object":{"C":13.75,"b%":62,"bV":3.86,"ph":3.2,"uS":402}}

and this is as far as I have got with the sensor:

- platform: mqtt
  name: h2o1_waterq_ph
  state_topic: application/10/device/115cf59123ed86c7/event/up
  value_template: '{{ value_json.ph | int }}'
  unit_of_measurement: 'pH'

Any pointers would be greatly appreciated

Thanks

Are you sure you pasted the payload correctly? In its current form, it’s an invalid JSON string because it missing a ]} at the end.

This is a valid JSON string (that has been “beautified”). Notice the ]} at the end.

{
	"applicationID": "10",
	"applicationName": "WaterQuality",
	"deviceName": "HT01",
	"devEUI": "115cf59123ed86c7",
	"rxInfo": [
		{
			"data": "haFDykFcAACicGjKQEzMzaJ1U80BkqJiVspAdwo9omIlPg==",
			"object": {
				"C": 13.75,
				"b%": 62,
				"bV": 3.86,
				"ph": 3.2,
				"uS": 402
			}
		}
	]
}

Using that valid JSON string, the correct JSON path to get the value of ph is the following:

  value_template: '{{ value_json.rxInfo[0].object.ph }}'

That template will return 3.2 but if you don’t want the fractional portion then use an int filter:

  value_template: '{{ value_json.rxInfo[0].object.ph | int }}'

Hi, Thanks so much for taking the time look and respond. My full MQTT payload is:

{
  "applicationID": "10",
  "applicationName": "WaterQuality",
  "deviceName": "HT01",
  "devEUI": "115cf59123ed86c7",
  "rxInfo": [
    {
      "gatewayID": "c0ee40ffff29736d",
      "uplinkID": "70bf0848-fd7d-4411-b9ad-0ec5f668ae57",
      "name": "Laird1i",
      "rssi": -59,
      "loRaSNR": 10.5,
      "location": {
        "latitude": xx.xxxx,
        "longitude": -xx.xxxx,
        "altitude": 0
      }
    }
  ],
  "txInfo": {
    "frequency": 867100000,
    "dr": 0
  },
  "adr": true,
  "fCnt": 0,
  "fPort": 2,
  "data": "haFDysL+AACicGjKQPVwpKJ1U9H/AaJiVspAc9cKomIlOA==",
  "object": {
    "C": -127,
    "b%": 56,
    "bV": 3.81,
    "ph": 7.67,
    "uS": -255
  }
}

should it therefore not be:

'{{ value_json.object.ph | int }}'

yea, that’s it! Thank you so much!

That payload’s structure is different from the first one. If you are certain the second one is correct then, yes, this template should work:

{{ value_json.object.ph | int }}

If it doesn’t then try it using bracket notation:

{{ value_json["object"]["ph"] | int }}

If it still fails to work then the payload’s actual structure may be different.

it is working great! Thanks again for your help…

1 Like

I had to use {{ value_json["object"]["b%"] | int }} as the battery % had a % in which obviously it didn’t like…

I just wanted to say though that it is people like you that make this one of the greatest ecosystems I have been part of… apart from some silly html 20 years ago, I had no modern coding experience. I have learnt so much from people like you, and just wanted to say thank you again…

1 Like