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:
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.
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.
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.
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.
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.