How to check availability in a mqtt-sensor

I have several mqtt sensor set-up; to prevent utility-meters from messing up if the device gets unavailable I would like to add a availability check. However, I can’t figure out how to do this. Can somebody point me in the right direction:

mqtt:
  sensor:    
    - name: "Slimme meter netto verbruik"
      state_topic: "youless_sm_pv"
      value_template: "{{ value_json.counter }}"
      unit_of_measurement: 'kWh'

Unless you set expire_after, the state will never expire and become unavailable. Is it only unavailable when HA is restarted? If so, you could create an automation that listens for the “youless_sm_pv” message and republish it under a different topic with the retain flag set (eg. topic “youless_sm_pv/retained”). Then change your sensor to instead pick up this new topic. When HA is restarted, the retained message will be received on connection and the sensor will be updated to the old value.