MQTT sensor receiving inconsistent JSON strings

Hi all - I am trying to setup an MQTT sensor, but I receive inconsistent JSON. It has to do with the way my original source is sending JSON.

So if i subscribe to the topic, i see the following. The JSON that is being sent is not always the saem.

weewx/loop {"BatteryStatus": "0.0", "cloudbase_foot": "5224.22544052", "hourRain_in": "0.0", "heatindex_F": "57.9", "outHumidity": "50.0", "windSpeed_mph": "0.0", "rain24_in": "0.0", "humidex_F": "57.9", "rainRate_inch_per_hour": "0.0", "dateTime": "1552063862.0", "dayRain_in": "0.0", "dewpoint_F": "39.3740490698", "outTemp_F": "57.9", "appTemp_F": "55.5590013822", "windchill_F": "57.9", "usUnits": "1.0"}
weewx/loop {"hourRain_in": "0.0", "rain24_in": "0.0", "rainRate_inch_per_hour": "0.0", "dateTime": "1552063865.0", "dayRain_in": "0.0", "CrawlSpaceHumidity": "43.0", "CrawlSpaceTemp_F": "64.4", "usUnits": "1.0"}
weewx/loop {"rain24_in": "0.0", "hourRain_in": "0.0", "LivingRoomTemp_F": "68.54", "rainRate_inch_per_hour": "0.0", "dateTime": "1552063868.0", "dayRain_in": "0.0", "usUnits": "1.0", "LivingRoomHumidity": "36.0"}
weewx/loop {"hourRain_in": "0.0", "BasementHumidity": "50.0", "rain24_in": "0.0", "rainRate_inch_per_hour": "0.0", "dateTime": "1552063875.0", "dayRain_in": "0.0", "BasementTemp_F": "65.48", "usUnits": "1.0"}
weewx/loop {"BatteryStatus": "0.0", "hourRain_in": "0.0", "windSpeed_mph": "0.0", "rain24_in": "0.0", "rainRate_inch_per_hour": "0.0", "dateTime": "1552063881.0", "dayRain_in": "0.0", "rain_in": "0.0", "rain_total": "114.27", "usUnits": "1.0"}

As a result, I have setup MQTT sensors that look like this:

  - platform: mqtt
    name: "Temperature"
    state_topic: "weewx/loop"
    value_template: "{{ value_json.outTemp_F }}"
    expire_after: 120
    device_class: temperature

  - platform: mqtt
    name: "Dew Point"
    state_topic: "weewx/loop"
    value_template: "{{ value_json.dewpoint_F }}"
    expire_after: 120
    device_class: temperature

  - platform: mqtt
    name: "Heat Index"
    state_topic: "weewx/loop"
    value_template: "{{ value_json.heatindex_F }}"
    expire_after: 120
    device_class: temperature

It grabs the values as intended which is good, however, since the JSON isn’t consistent, I see gaps in the sensors, and as a result, the main sensor will sometimes show no data. I thought by putting “expire_after” would solve the issue but it didn’t.

2019-03-08_12-20-50

Any ideas of how I can get the gaps here to actually not fully reset to 0? I can’t split the JSON into different topics, due to the way that the source is sending the JSON across.

I think the trick is to make the value_template more sophisticated. It should check if the value it is extracting is defined. If it’s defined, report it. If it’s not defined, report the current value (which should fill in the gaps you’re now experiencing).

Apologies but I don’t have time to fully test this but it should give you an idea on how to approach the problem.

  - platform: mqtt
    name: "Dew Point"
    state_topic: "weewx/loop"
    value_template: >-
      {% if value_json.dewpoint_F is defined %}
        {{ value_json.dewpoint_F }}
      {% else %}
        {{ states('sensor.dew_point') }}
      {% endif %}
    expire_after: 120
    device_class: temperature