Hold/retain sensor value between mqtt messages -- SOLVED! THX

I’ve got an SDR grabbing messages from my weather station, putting them into mqtt, and I’m bringing them into HA… there are 3-5sec between messages… the issue I’m trying to fix is that my values come in, but once they are there, they change to 0 until the next message… I want to retain or hold the value until the next change… here’s the code I’m using (in a packages format)… what change should I make? what’s it called? I couldn’t find it on my own… please guide me in the right direction, and thx in advance…

I’m guessing that you get separate messages for “wind_avg_mi_h” and “wind_dir_deg”, but both use the same MQTT topic of “/Acurite-Atlas-857/state”. And you’re finding that receiving a message for one clears the other.

If this is the case, you can achieve what you want with a template sensor using a trigger. Therefore the code moves from being under mqtt: to being under template: (probably in templates.yaml), and becomes:

- trigger:
    - platform: mqtt
      topic: "mb/test"
      payload: "True"
      value_template: "{{ value_json.a > 0 }}"
  sensor:
    - unique_id: mqtt_test_a
      name: "MQTT Test A"
      state: >-
        {{ trigger.payload_json.a }}

- trigger:
    - platform: mqtt
      topic: "mb/test"
      payload: "True"
      value_template: "{{ value_json.b > 0 }}"
  sensor:
    - unique_id: mqtt_test_b
      name: "MQTT Test B"
      state: >-
        {{ trigger.payload_json.b }}

This creates two sensors that both trigger from MQTT topic “mb/test”, but they only update if the value_template evaluates to True. This means that if the payload contains { "b": 12 }, then the trigger for a` will fail, and that sensor will not update (i.e. it will retain the old value). Obviously you need to change my example topic and properties to match yours.

BTW it’s also not considered best practice to have a leading slash on the topic, but I’m assuming this is not in your control.

ah, that makes more sense… I get 3 different messages from my station, lightening count & wind direction are the only sensors that are in every message… all other sensors are spread out throughout the messages… so based on the method I’m using, for example, when temperature is sent out, mqtt populates my HA sensor with a value… but as soon as its not in the other messages, it turns to a 0 value…

so by triggering a template, I can get mqtt to hold the value until the next message with a different value…

now, something interesting… this needs to be in a templates.yaml… currently, using separate files, I have these sensors in a weather_packages.yaml… so I need to see if templates must be in the same file or if I can package them up…

that gives me a direction to go… thanks…

I don’t know if its because of the version or what, but if I used this syntax, the values are “unavailable”… if I add that extra “/”, it works… go figure…

topic: “/mb/test”

thanks a bunch…

got this all worked out… here is what I got the syntax changed to, hope it helps someone else…

the only other thing I changed was to change the “> 0” condition to “>= 0” so there can be a value of 0… prior, it would give a value of “unknown” until > 0…

also, I can verify that this syntax works in packages… so I have a packages folder and acurite_atlas.yaml package file with only these template/trigger entities… works great…

hope it helps someone…

screenshot
screenshot2