MQTT Missing Data breaking Utility Meter

I am trying to setup a rain tracking utility meter on my weather station but the mqtt message alternates the data sent back in the message where sometimes it contains rain_mm data and other times it doesn’t.

How can I adjust my configuration to use the previous value if the data doesn’t exist in the mqtt paylod?

Payload 1:

{
  "time": "2023-01-16 09:16:41",
  "model": "Acurite-5n1",
  "message_type": 56,
  "id": 1558,
  "channel": "C",
  "sequence_num": 2,
  "battery_ok": 1,
  "wind_avg_km_h": 4.3112,
  "temperature_C": 12.38889,
  "humidity": 99,
  "mic": "CHECKSUM"
}

Payload 2:

{
  "time": "2023-01-16 09:16:59",
  "model": "Acurite-5n1",
  "message_type": 49,
  "id": 1558,
  "channel": "C",
  "sequence_num": 2,
  "battery_ok": 1,
  "wind_avg_km_h": 5.139,
  "wind_dir_deg": 270,
  "rain_mm": 95.758,
  "mic": "CHECKSUM"
}

I have a feeling the gaps in the data are what’s breaking the utility meter component.

Current rain sensor in mqtt config

- name: Weather Station Rain Meter Cumulative
  unique_id: weather_station_rain_meter_cumulative
  icon: mdi:weather-rainy
  state_topic: "rtl_433/Acurite-5n1/1558"
  unit_of_measurement: "mm"
  value_template: "{{value_json.rain_mm}}"

Try this:

- name: Weather Station Rain Meter Cumulative
  unique_id: weather_station_rain_meter_cumulative
  icon: mdi:weather-rainy
  state_topic: "rtl_433/Acurite-5n1/1558"
  unit_of_measurement: "mm"
  value_template: "{{ value_json.rain_mm if value_json.rain_mm is defined else this.state }}"

Great thanks! This seems to work well but it stopped raining so i can’t tell if it worked :smiley: