MQTT Sensor / Template: How to "hold" value until next correct value arrives?


I have an MQTT sensor set up like this:

- platform: mqtt
  name: "rtl_433_Temperature1"
  state_topic: "stat/rtl_433"
  value_template: "{% if == 231 and value_json.model == 'Nexus Temperature' %} {{ value_json.temperature_C }} {% endif %}"

It works fine, displaying the correct temperature whenever it is being distributed via MQTT. However, the sensor only holds that value until another MQTT message with another ID - not matching the if-statement in the template - arrives. Then it is set to NULL. How can I get the sensor to keep the value until it reads a new value matching the if statement?


Just give the sensor it’s own value.

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

Thanks! This is plausible and working!