Value_template question

getting temperature sensor info from mqtt
in the line there is an entity with an ‘id’ and temperature_C
trying to only get info from the correct sensor (and failing)
so far got:

value_template: '{%if (value_json.id == '247' %) }{{ value_json.temperature_C }}'

any ideas??

Please provide the entire json and the template for someone to help.

here is the recieved topic:

Client mosqsub/8670-dadraspi3 received PUBLISH (d0, q0, r0, m0, 'home/rtl_433', ... (157 bytes)) {"time" : "2017-06-20 12:22:14", "brand" : "OS", "model" : "THGR810", "id" : 247, "channel" : 1, "battery" : "OK", "temperature_C" : 28.000, "humidity" : 46} Client mosqsub/8746-dadraspi3 received PUBLISH (d0, q0, r0, m0, 'home/rtl_433', ... (101 bytes)) {"time" : "2017-06-20 12:27:08", "model" : "LaCrosse TX Sensor", "id" : 18, "temperature_C" : 25.000}
and the sensor

[code]sensor 5:

  • platform: mqtt
    state_topic: ‘home/rtl_433’
    name: ‘Oudoor Temperature’
    unit_of_measurement: ‘°C’
    value_template: ‘{{ value_json.temperature_C }}’[/code]
    the above works, but not with the code I am asking about

It’s a wild guess here, because I can’t really tell what you are asking, BUT, there are a few problems with the partial code in your first post. I have fixed those, and made a few guesses about what you want. I’ve also added some test code that you can paste into the templating developer tool to play with so you can see how it works. Your big problem was including quotes around the test for the id. Hope this helps.

{%set value_json = {"time" : "2017-06-20 12:22:14", "brand" : "OS", "model" : "THGR810", "id" : 247, "channel" : 1, "battery" : "OK", "temperature_C" : 28.000, "humidity" : 46} %}
     {%if (value_json.id == 247)%} 
     {{ value_json.temperature_C }}
     {%else%}
     1000
     {%endif%}
1 Like

Thanks for the help.
There are 2 sensors, both reporting with the same topic? name (temperature_C)
outdoor id = 247
indoor sensor id = 18

Just wanted to chime in and say thanks for this template example.
As a pretty new ha user and this was something I was trying to figure out last night myself.

I had this issue since I have a stream of json comming into the mqtt broker from a rtl-sdr stick scanning with the rtl_433 software and dumping all the sensor data it grabs of the air. Its all in the same topic and only way to differenciate them is by the id.

I now sample all the wireless temp sensors in my neighbourhood, just have to figure out which is the ones I want now.

I am still unsure why the data is getting overwritten.
I have a reply from the author of rtl_433

https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!topic/rtl_433/0096Eyzq5Js

I have also refined the template

value_template: ' {%if (value_json.id == 247) and (value_json.temperature_C != "")%} {{ value_json.temperature_C }} {%endif%} '
but data is still overwitten
next, I intend to intercept the data from rtl_433 with a python script, get the messages, re-assemble them and then subscribe them to mqtt.

just an aside, with a simple script, I read the mqtt topic there is an error when trying to decode it:

data = json.loads(message.payload) TypeError: the JSON object must be str, not 'bytes' but it works if data = message.payload data = data.decode('ascii')

I think what is happening is that the state is getting updated even when the if fails. To fix this, create an else part that fills in the current state.

sensor test:
  - platform: mqtt
    state_topic: 'home/rtl_433'
    name: 'Oudoor Temperature'
    unit_of_measurement: '°C'
    value_template: '{%if (value_json.id == 247)%} {{ value_json.temperature_C }}{%else%}{{states.sensor.oudoor_temperature.state}}{%endif%}' 

This will mean the state gets updated every time to receive a message with that topic. Ideally you want a ‘no change’ in the else section, but I don’t know if that is possible.

Thanks very much for that
It works for one sensor, but not another
wierd!!!

part of the problem ,is that the initial state in NULL until there is a message from mqtt

see

This script would be very interesting to see and have sorting thru a rtl-433 mqtt feed.
If you live in a bit of a crowded RF space with multiple sensors this gets abit unwruly fast.

It is really awesome to have a pick and choose from all the “free” sensors in my neighborhood.