MQTT message filtering with template

Hi,
I am trying to integrate sensor data from rtl_433, which can provide MQTT messages from a large range of wireless weather sensors, thermometers etc. HASS currently assumes (AFAIK) that messages from each sensor is nicely divided into separate topics. Unfortunately that is not the case for rtl_433, where the topic value contains a JSON string including the type and ID of the sensor.
e.g.
home/rtl_433 {“time” : “2018-02-11 15:03:07”, “model” : “Fine Offset Electronics, WH25”, “id” : 229, “temperature_C” : 21.100, “humidity” : 29, “pressure” : 977.500}

Therefore it would be nice to have the possibility to further filter messages based on the payload of the message with a template:

filter_template
(template)(Optional)Defines a template to filter messages based on payload content.
Template must resolve to the boolean value true for the message to be decoded.
Default value: {{ true }}

Example:
  - platform: mqtt
    name: "Outdoor Temperature"
    state_topic: "home/rtl_433"
    unit_of_measurement: "°C"
    filter_template: "{{ value_json['model'] == 'Fine Offset Electronics, WH25' and value_json['id'] == 229 }}"
    value_template: "{{ value_json['temperature_C'] }}"

you can do this already with multiple mqtt sensors reading the same data:

  - platform: mqtt
    name: 'Local Temperature'
    state_topic: 'home/rtl433/weather'
    unit_of_measurement: '°C'
    value_template: '{{ value_json.temperature_C }}' 
  - platform: mqtt
    name: 'Local Humidity'
    state_topic: 'home/rtl433/weather'
    unit_of_measurement: '%hm'
    value_template: '{{ value_json.humidity }}'
  - platform: mqtt
    name: 'Weather Station Battery'
    state_topic: 'home/rtl433/weather'
    value_template: '{{ value_json.battery }}'
    icon_template: >
      {%- if states("sensor.weather_station_battery") != 'OK' -%}
        mdi:battery-alert
      {%- else -%}
        mdi:battery
      {%- endif -%}

Hi @vestom, you can do the filtering with the value_template.

  - platform: mqtt
    state_topic: "home/rtl_433"
    name: "Outside Temp Nexus"
    unit_of_measurement: "°C"
    value_template: >
      {% if value_json is defined and value_json.id == 137 %}
        {{ value_json.temperature_C }}
      {% else %}
        {{ states('sensor.outside_temp_nexus') }}
      {% endif %}
1 Like

Hi,
Yes, that is what I currently do. However it becomes very unwieldy with many sensors with multiple values. Furthermore it will trigger a state update, even though it was not the right sensor.

I have tried to make a proof of concept implementation that works well for me. If it has general interest, I am happy to submit a pull request (including documentation update)
https://github.com/vestom/home-assistant/tree/mqtt_filter
/Tommy

Yeah, but it is not quite my problem. The issue is having messages from different sensors with same topic, where the sensor is identified by a payload value.

I sorted this by changing the rtl_433 script so it checks the model field of the json package and then submits to a different topic, and in HA the sensors read from the different topics if that makes sense.

I have the same problem. Currently I solved by using a NodeRed flow, found it very intuitive and easy to mantain.


Screenshot%20from%202018-02-11%2019-35-15

I also needed a RBE block to filter repetitive commands, I’m receiving from 433MHz remotes and keeping the button down repeats the transmission too many times.

Nonetheless, I’m really interested in seeing @jchasey solution. Can you post the rtl_433 script, or just the changes you made?

1 Like

Yes here is the main loop with the changes:

/usr/local/bin/rtl_433 -F json $PROTOCOL | while read line
do
	# We don't know from the output which protocol number was received
	# However we can extract the device name from json output
	DEVICE="$(echo $line | jq --raw-output '.model')"
	
	if [ "$DEVICE" = "HIDEKI TS04 sensor" ]; then
	  MQTT_TOPIC="home/rtl433/weather"
	fi
	if [ "$DEVICE" = "Oil watchman" ]; then
	  MQTT_TOPIC="home/rtl433/oil"
	fi

	# Publish the json to the appropriate topic
	echo $line | /usr/bin/mosquitto_pub -h $MQTT_HOST -u $MQTT_USER -P $MQTT_PASS -i RTL_433 -r -l -t $MQTT_TOPIC
done
2 Likes