Json question for new sensor

@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…

Well before when i had:

states("sensor.soil_01_adc")

The temperature from soil_1 would add itself to the value of soil_2, and vice versa giving me strange readings.

By trying to solve this issue and fumbling around, this actually appeared to solve that problem and now it all appears to be working except for the rounding.
But like i said i don’t know what i’m doing and just tried something and thought i got lucky…

Another way of explaining is that i have multiple sensors and their messages all come from the same gateway publishing them all the same way, and the means to distinguish what value goes to what sensor was to use the “name” filter as suggested here earlier by another user.

That worked well, but then i noticed the temperature of one sensor somehow was added to the next sensor and vice versa, so the reading were one right one doubled up, one right , one doubled up etc…

I tried changing it to:

is_state_attr("sensor.soil_01_adc")

and the problem stopped, so i figured it worked, except for the rounding…

Does that make sense?

Thanks!

see here…

{{ is_state_attr(“sensor.soil_01_Temperature”) | round(2) }}
{{ state_attr(‘sensor.appdaemon_memory_average’, ‘max_value’) | round(1)}} works for me… rounds to 1
For is_state_attr you must define an attribute and a value. See the above referenced doc.

i tried this:

- 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).tempc }}
    {% else %}
      {{ state_attr("sensor.soil_01_Temperature")|round(2) }}
    {% endif %}
  unit_of_measurement: '°C'

And it seems to preserve my “fix” of temperature affecting the next sensor.

Does this look better to you?
The rounding still didn’t work for me tho…

Thanks for your help!