Json question for new sensor

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!

You still haven’t said what attribute you want… and it looks like you are referencing the sensor you are creating in a value_template that is creating the sensor! - how does sensor.soil_01_Temperature even exist when the same value_template is creating it? This is an impossible loop.

What should i do?
how do i remedy this?

Thanks

well what is the state topic and what do you want to extract from it and what conditions are there?

Here i captured a message from the gateway (happens to be Soil_04):

{"rssi":-87,"snr":9.25,"pferror":15493,"packetSize":60,"message":"{\"name\":\"Soil_04\",\"tempc\":11.64284,\"hum\":82.26128,\"adc\":691}"}

So i basically want to parse the sensor name to send sensor Soil_04 data to home assistant as Soil_4 temp, hum and adc
All the messages come in the same so the name is the distinguishing factor.

What i was doing was suggested by another user to filter using the name, and it seemed to work.

Does that make sense?

Thank you!

doesn’t each sensor use a different topic?

no, because they communicate using lora, and the gateway passes the message on to home assistant as shown in the previous posting.
Another user suggested I uncomment my “topic” in the arduino code, but that gets embedded in the message as well, so a filter again would be used to extract it (i think).

Thanks for your help!

No it isn’t. It’s a filter for the incoming MQTT messages which share the same topic for multiple devices: read from the start. Whilst I agree that using state_attr doesn’t make sense, the structure of the template is fine.

Works thus: if the incoming JSON has a name of Soil_01, take the tempc as the new state; otherwise return the existing state (a “do-nothing” step).

I really don’t understand where the addition problem is coming from, though.