Json question for new sensor

Well now we’re getting there.
Try this:

- platform: mqtt
  name: "Soil_1_ADC"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: '{{ value_json.message.adc }}'
  json_attributes_topic: "home/OMG_01/LORAtoMQTT"
  json_attributes_template: "{{ value_json | tojson }}

We will need to make a second sensor to get the other messages but I think this should extract the adc into the state and all the attributes will be the same as they are now.

Again show the dev-tools state.

Also if you can give me the FULL JSON from the broker I can have a play with it here.

Unfortunately that didn’t do it…

I get the same as the previous screenshot, but this time the state field is blank.

As far as the full json i’m not sure where i can capture that information… how can i get that to you?

Thanks

this maybe?

  • Received 8:08:57 PM

    • QoS: 0
    • Payload:

rssi: -36 snr: 10 pferror: 12171 packetSize: 93 message: >- {“id”:“LoRaADC”,“name”:“Soil_1”,“model”:“LSMS092D”,“tempc”:21.16318,“hum”:46.95463,“adc”:879}

The state from the previous screenshot is the full json string.

- platform: mqtt
  name: "Soil_1_ADC"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: '{{ value_json }}'

from this… the state is the full json. Then I can use the template editor to work it out. Like I said I always struggle with this shit extracting data but the template editor reveals all

Well i appreciate you taking the time!

That configuration gave me this:

really???
I want whatever this says:
image
Is the broker not showing that topic anymore?

actually it popped up here:

{‘rssi’: -37, ‘snr’: 9.75, ‘pferror’: 11903, ‘packetSize’: 93, ‘message’: ‘{“id”:“LoRaADC”,“name”:“Soil_1”,“model”:“LSMS092D”,“tempc”:21.87061,“hum”:47.08147,“adc”:879}’}

If you use the free MQTT Explorer you should see it like this:


And you can copy out of there

ok will try. does this work though?

{‘rssi’: -37, ‘snr’: 9.75, ‘pferror’: 11903, ‘packetSize’: 93, ‘message’: ‘{“id”:“LoRaADC”,“name”:“Soil_1”,“model”:“LSMS092D”,“tempc”:21.87061,“hum”:47.08147,“adc”:879}’}

well it’s failing in the json validator

Let me double check that it’s not a copy pasta

Here it is from mqtt explorer:

{"rssi":-37,"snr":10,"pferror":12272,"packetSize":93,"message":"{\"id\":\"LoRaADC\",\"name\":\"Soil_1\",\"model\":\"LSMS092D\",\"tempc\":21.93432,\"hum\":46.93069,\"adc\":879}"}

ok so in the message, I seeimage before/after every key. Why? How did you create this? They should just be normal quotes if they are a string. The whole format is also using invalid single quotes it seems (Or maybe copy/paste is causing that here). It should look like this:
{“rssi”:-37,“snr”:10,“pferror”:12272,“packetSize”:93,“message”:{“id”:“LoRaADC”,“name”:“Soil_1”,“model”:“LSMS092D”,“tempc”:21.93432,“hum”:46.93069,“adc”:879}}

Checking the format:
image

Only took 32 posts to work out your format is wrong lol

As far as the image i’m not sure how that got in there maybe @1technophile 's OpenMqttGateway injected it? Or maybe it’s my arduino code?

Here is a screenshot of mqtt explorer to ensure it’s not a copy pasta issue…

Thank you again for helping me with this!

Yeah they are screwing up the message data. those characters are what you usually inject to escape things in linux but they are screwing up the message string. You need to get rid of those to read the data in correctly… unless someone else has a different way

yikes.
OK i’ll keep digging then…

Thank you!

1 Like

There is hope!
I found a similar issue here:

So i’ll read through it all and try and figure out the solution and report back.

Thanks!

1 Like

Thinking about this some more, it might be possible to filter the value_json to remove the extra crap… I’ll see tomorrow…

@Refuge When posting code (including MQTT messages) please surround it with backticks (key near Escape) or use the Preformatted text button in the editor.

At least one of your issues above is due to copying the “smart quotes” from a post into your code.

Look at the difference in quotes: “rssi” versus "rssi". The first one has been “enhanced” to use “66/99” quotes for easier reading by humans, but this totally screws up code.

Here’s what you need — paste into the template editor to see what’s going on. The variable a has the MQTT Explorer result you (correctly) pasted a few posts up.

{% set a = {"rssi":-37,"snr":10,"pferror":12272,"packetSize":93,"message":"{\"id\":\"LoRaADC\",\"name\":\"Soil_1\",\"model\":\"LSMS092D\",\"tempc\":21.93432,\"hum\":46.93069,\"adc\":879}"} %}
{{ a.rssi }} <-- gives -37
{{ a.message }} <-- gives a JSON-style string
{{ a.message|from_json }} <-- gives a data structure
{{ (a.message|from_json).id }} <-- gives LoRaADC

Thank you @Troon this confirms my suspicion!
I also suspected that the backticks were the way to go. I’ll continue do that from now on.

I appreciate all your advice! Thanks