Need help on value_template

I have a weather station output the below JSON via MQTT. I have 2 temp sensors reporting in 2 channels, each channel for each room. Since the weather station output is for 2 channels, I need to compare the channel and find out the room temperature.

{“dev”:“digoo”,“id”:15,“ch”:3,“batt”:1,“temp”:32.8,“hum”:62,“raw”:“”}
{“dev”:“digoo”,“id”:9,“ch”:2,“batt”:1,“temp”:31.1,“hum”:62,“raw”:“”}

My sensor config is below:

- platform: mqtt
  name: "Bedroom Temperature"
  state_topic: "home/WeatherStation"
  unit_of_measurement:"'C"
  value_template:>
    {% if ('{{value_json.ch}}',2) %}
    {{ value_json.temp }}
    {% endif %}
- platform:mqtt
  name: "Hall Temperature"
  state_topic: "home/WeatherStation"
  unit_of_measurement: "'C"
  value_template:>
    {% if ('{{value_json.ch}}',3) %}
    {{ value_json.temp }}
    {% endif %}

But the homeassistant shows only 1 output, which it got the latest. For example if the 1st one’s output is 29.3C then it shows 29.3 and 2nd one’s output is 32.1C then it shows 32.1

Even-though both are different sensors, the output of both is same.

Any help on fixing the config is much appreciated.

Take a look here:

1 Like

I am using this version of rtl_433
rtl_433 version 18.12-88-ga9a327d branch master at 201902080905

I run this command to send the data to my mqtt broker (server)
rtl_433 -R 40 -R 12 -F json -M utc | mosquitto_pub -h mqtt_ip-address -p mqtt-port -u mqtt-username -P mqtt-password -t home/rtl_433 -l

In HA, I have a directory called sensors.
Inside that, I seprated each sensor:

OregonRain.yaml

- platform: mqtt
  state_topic: 'home/rtl_433'
  name: 'Rain Total'
  unit_of_measurement: ''
  value_template: >
    {% if value_json is defined and value_json.id == 151 %}
      {{ value_json.rain_total }} 
    {% else %}       
      {{ states('sensor.rain_total') }}
    {% endif %}
    
- platform: mqtt
  state_topic: 'home/rtl_433'
  name: 'Rain Rate'
  unit_of_measurement: 'in/hr'
  value_template: >
    {% if value_json is defined and value_json.id == 151 %}
      {{ value_json.rain_rate }} 
    {% else %}       
      {{ states('sensor.rain_rate') }}
    {% endif %}  

OregonTemp.yaml

- platform: mqtt
  state_topic: "home/rtl_433"
  name: "Outdoor Humidity"
  unit_of_measurement: '%'
  value_template: >
    {% if value_json is defined and value_json.id == 55 %}
      {{ value_json.humidity }}
    {% else %}
      {{ states('sensor.outdoor_humidity') }}
    {% endif %}
    
- platform: mqtt
  state_topic: "home/rtl_433"
  name: "Outdoor Temperature"
  unit_of_measurement: '°C'
  value_template: >
    {% if value_json is defined and value_json.id == 55 %}
      {{ value_json.temperature_C }}
    {% else %}
      {{ states('sensor.outdoor_temperature') }}
    {% endif %}  

OregonWind.yaml

- platform: mqtt
#wind gust
  state_topic: "home/rtl_433"
  name: "Wind Gust"
  unit_of_measurement: 'm/s'
  value_template: >
    {% if value_json is defined and value_json.id == 187 %}
      {{ value_json.gust }}
    {% else %}
      {{ states('sensor.wind_gust') }}
    {% endif %}    
    
- platform: mqtt
  state_topic: 'home/rtl_433'
  name: 'Wind Direction'
  unit_of_measurement: ''
  value_template: >-
    {% if value_json is defined and value_json.id == 187 %}
      {% set wd = value_json.direction | int %}
      {% if wd <= 11 or wd > 348 %}North
      {% elif wd >11 and wd <=34 %}North North East
      {% elif wd >34 and wd <=56 %}North East
      {% elif wd >56 and wd <=79 %}East North East
      {% elif wd >79 and wd <=101 %}East
      {% elif wd >101 and wd <=124 %}East South East
      {% elif wd >124 and wd <=146 %}South East
      {% elif wd >146 and wd <=169 %}South South East
      {% elif wd >169 and wd <=191 %}South
      {% elif wd >191 and wd <=214 %}South South West
      {% elif wd >214 and wd <=236 %}South West
      {% elif wd >236 and wd <=259 %}West South West
      {% elif wd >259 and wd <=281 %}West
      {% elif wd >281 and wd <=304 %}West North West
      {% elif wd >304 and wd <=326 %}West North West
      {% elif wd >326 and wd <=348 %}North North West
      {% endif %}
    {% else %}       
      {{ states('sensor.wind_direction') }}
    {% endif %} 

to get the id (value_json.id == 151 %)

mosquitto_sub -h mqtt_ip-address -p mqtt-port -u mqtt-username -P mqtt-password -t ‘#’ -v

you will get:

home/rtl_433 {“time” : “2019-04-08 09:42:58”, “brand” : “OS”, “model” : “PCR800”, “id” : 151, “channel” : 0, “battery” : “OK”, “rain_rate” : 0.000, “rain_total” : 1.278}

2 Likes

the way I did it was simply adding [/id] or [/channel] into your MQTT rtl_433 command

rtl_433 -F "mqtt://myrouter.local:1883,retain=0,events=rtl_433[/model][/id][/channel]

then you will get individual copics for each ID and Channel, creating sensor for each is simple as adding the id or channel to the end of the sensor setting e.g.

  - platform: mqtt
    state_topic: 'rtl_433/model/id/channel'
    name: 'Outside Temperature °C'
    unit_of_measurement: '°C'
    value_template: '{{ value_json.temperature_C }}'

recommend using MQTT Explorer to see how the JSON flows on your network and confirm you are getting expected information out of rtl_433