Json question for new sensor

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

@DavidFW1960 what would you advise? fiddling with the arduino code to get it out better? or trying to remove “extra crap”?

Yesterday I did play with the arduino code and got this before i gave up :

{"rssi":-46,"snr":9.5,"pferror":12993,"packetSize":30,"message":"tempc: 21.18hum: 49.17adc: 731"}

But I wasn’t able to add a spaces or quotes… (i’m a total noob when it comes to code; thanks for bearing with me)

Let me know what you think is best.
Thanks!

Edit: copied form mqtt explorer

Edit:
@Troon wait what?


Yeah i think this is what we were after!

How can i apply this to my yaml?

I tried:

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

and

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

Neither of which worked…

Thanks

If you want the ADC value within the message, try:

- platform: mqtt
  name: "Soil_1_ADC"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: '{{ (value_json.message|from_json).adc }}'

Amazing! That did it. Thank you!

Is there a way to have all the sensors grouped like David’s coffee maker shown here earlier?

This is so awesome I can’t wait to make a posting to share with the community!!!
Thanks!

Here it is:

1 Like

Since this worked I got a few more sensors; is there any way to distinguish Soil_1_ADC from Soil_2_ADC using the name identifier in the json message?

Thanks in advance!

I would have thought something like this would work:

- platform: mqtt
  name: "Soil_1_ADC"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_1" %}
      {{ (value_json.message|from_json).adc }}
    {% else %}
      {{ states("sensor.soil_1_adc") }}
    {% endif %}

- platform: mqtt
  name: "Soil_2_ADC"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_2" %}
      {{ (value_json.message|from_json).adc }}
    {% else %}
      {{ states("sensor.soil_2_adc") }}
    {% endif %}

The idea (totally untested) is that it checks the incoming message on the topic, and if the name matches it updates the sensor with the adc value, otherwise it “updates” it with the existing value.

Yep that worked!
Thank you so much!

Actually, not sure where this is coming from but now it behaves strangely.
At first everythig appeared ok, then i noticed strange jumps in values.
The values seem to add up at times.

let’s say get 70 on soil_1
and had a reading of 70 on soil_2

Then i press the reset button on soil_1, I get 70 on soil_1 and 140 on soil_2

If i press reset on soil_2 i get 70, and on soil_1 i get 140

Is this a json issue or an issue coming from openmqttgateway?

here’s what i’m doing:

- platform: mqtt
  name: "Soil_1_ADC"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_1" %}
      {{ (value_json.message|from_json).adc }}
    {% else %}
      {{ states("sensor.soil_1_adc") }}
    {% endif %}
  unit_of_measurement: 'µS/cm'

- platform: mqtt
  name: "Soil_1_Humidity"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_1" %}
      {{ (value_json.message|from_json).hum }}
    {% else %}
      {{ states("sensor.soil_1_Humidity") }}
    {% endif %}
  unit_of_measurement: '%'

- platform: mqtt
  name: "Soil_1_Temperature"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_1" %}
      {{ (value_json.message|from_json).tempc }}
    {% else %}
      {{ states("sensor.soil_1_Temperature") }}
    {% endif %}
  unit_of_measurement: '°C'

- platform: mqtt
  name: "Soil_2_ADC"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_2" %}
      {{ (value_json.message|from_json).adc }}
    {% else %}
      {{ states("sensor.soil_2_adc") }}
    {% endif %}
  unit_of_measurement: 'µS/cm'
  
- platform: mqtt
  name: "Soil_2_Humidity"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_2" %}
      {{ (value_json.message|from_json).hum }}
    {% else %}
      {{ states("sensor.soil_2_Humidity") }}
    {% endif %}
  unit_of_measurement: '%'

- platform: mqtt
  name: "Soil_2_Temperature"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_2" %}
      {{ (value_json.message|from_json).tempc }}
    {% else %}
      {{ states("sensor.soil_2_Temperature") }}
    {% endif %}
  unit_of_measurement: '°C'

Thanks in advance!

I can’t see how that behaviour can be coming from the sensor definitions you pasted. Have a look at what messages are on the MQTT bus using something like MQTT Explorer or if you have a free Pi / Linux machine on the network, install mosquitto and run:

mosquitto_sub -h YOUR_BROKER_IP -t "#" -v

Using mqtt explorer i get this:

{"rssi":-41,"snr":9.75,"pferror":15393,"packetSize":93,"message":"{\"id\":\"LoRaADC\",\"name\":\"Soil_1\",\"model\":\"LSMS092D\",\"tempc\":20.06016,\"hum\":46.40884,\"adc\":839}"}

Which looks fine and behaves as expected.
What’s interesting is that i’ve unplugged soil_2, and the values keep incrementing up. it’s now showing 11,896.34202 °F
It also seems that only temperature and possibly humidity are affected.

any ideas?
Thanks a million.

No, I’m lost on that one, sorry. There’s no addition in the templates you’re using, but as I said, I’ve not tested them myself.

Is your global setting to use °F? If not, seems odd that the sensor is configured for °C but reading in °F…

yikes.
ok well i’ll ask to see if it’s an openmqttgateway issue…

Yeah the °F comes from home assistant converting it automagically.

Thank you for your help!

Solved it!

I changed:

states("sensor.soil_01_adc")

to

is_state_attr("sensor.soil_01_adc")

So the sensors no longer affect each others’ behavior which is awesome!
Bu the rounding isn’t working, so there is still something buggy… but getting there!! i can finally test out several nodes at once!

here is the working code to support multiple sensors:

- platform: mqtt
  name: "Soil_01_ADC"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_01" %}
      {{ (value_json.message|from_json).adc }}
    {% else %}
      {{ is_state_attr("sensor.soil_01_adc") }}
    {% endif %}
  unit_of_measurement: 'µS/cm'
  icon: mdi:flower

- platform: mqtt
  name: "Soil_01_Temperature"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_01" %}
      {{ (value_json.message|from_json).temp_c }}
    {% else %}
      {{ is_state_attr("sensor.soil_01_Temperature") | round(2) }}
    {% endif %}
  unit_of_measurement: '°C'

- platform: mqtt
  name: "Soil_01_Humidity"
  state_topic: "home/OMG_01/LORAtoMQTT"
  value_template: >-
    {% if (value_json.message|from_json).name == "Soil_01" %}
      {{ (value_json.message|from_json).hum }}
    {% else %}
      {{ is_state_attr("sensor.soil_01_Humidity") | round(2) }}
    {% endif %}
  unit_of_measurement: '%'
  icon: mdi:water-percent

Thanks

I don’t think you can use is_state_attr like that… is_state_attr what? You haven’t given it any parameters firstly and it’s only going to return true/false not a number to round anyway…

lol, well i kinda have no idea what i’m doing, just throwing spaghetti at the the wall… what i do know is that it appears to be working as expected as far as i can tell… minus the rounding not working… and it has solved the issue of two sensors affecting each other…

But if there’s abetter what of doing it, i’m all ears!
Thanks in advance!

No it’s working ‘by default’ because the first condition is satisfied.

Not sure what that means… but it’s working right?
Or is there a better way of doing this?

Thanks for taking the time!

Your rounding isn’t working and that is why… What are you even trying to do with is_state… is is asking a question (yes/no) not returning something to the template value. So no by your own words it’s not working… you just think it is…