MQTT Sensor update and value retention post 2022.12

I don’t often use MQTT and the recent changes that remove configuration.yaml entries have me a bit perplexed.

The project is a remote, battery-powered propane tank sensor. I have the hardware built and the code seems to be working OK to read the value and publish the level via MQTT then go into deep sleep. The sleep values in the code are just for testing, once deployed I anticipate that I will wake the device once a week to report level then back to deep sleep.

Here is my YAML code (excluding all the standard stuff):

  id: deep_sleep_control
  run_duration: 3min
  sleep_duration: 2min

  port: 1883
  username: !secret mqtt_user
  password: !secret mqtt_password
  discovery: True
  discovery_retain: True
  client_id: propanemonitor
  topic_prefix: propanemonitor
    - topic: ota_mode
      payload: 'ON'
        - logger.log: 'OTA Mode ON - Deep sleep DISABLED'
        - deep_sleep.prevent: deep_sleep_control
    - topic: ota_mode
      payload: 'OFF'
        - logger.log: 'OTA Mode OFF - Deep sleep ENABLED'
        - deep_sleep.enter: deep_sleep_control

- platform: adc
  pin: A0
  name: "Propane Sensor"
  update_interval: 2min
    - multiply: 3.3
    - lambda: return x * (31.66360585143436) + (10);
  unit_of_measurement: "%"

The problem is that the device is waking, publishing the value (which I can see in my MQTT client) but the sensor is not updating in HA and when the device sleeps, it changes to ‘Unavailable’.

I have followed some of the other posts on here and I figure that the sensor needs ‘force_update’ as the value is not changing during testing but with the MQTT changes in HA, I cannot for the life of me, figure out how to do that. I’ve tried adding the code to my sensors.yaml file but the config check complains that it now needs to be done in the integration. I cant see a way to add it in the integration.

The message does seem to have the retain flag set…

Any suggestions?

Hmm… as usual I make some ‘progress’ within a few minutes of posting.

Following the HA docs, I tried the sensor code in the configuration.yaml and it worked to update the sensor:

    - name: "Propane Level"
      state_topic: propanemonitor/sensor/propane_sensor/state
      unit_of_measurement: "%"
      force_update: True

but if I put the same code in sensors.yaml file it throws an error. No idea what is going on there.

Just a guess, but the mqtt platform in the sensor domain ist deprecated and does not work anymore.
So you have to do it like you posted in the mqtt domain.
Look here where it is described better.

Thanks @madface. The link is interesting and seems like a good solution for someone that has lots of mqtt sensors. I just have the one. It seems counterintuitive to be pushing sections out of the configuration.yaml everywhere else but moving to push them into the config file for mqtt. I think, in reality, it’s just the way my mind is working it out as your link gives a good example of pushing out to a different sensor file for mqtt, albeit a file just for mqtt sensors and not sensors in general.

As always, I learned something here and I appreciate the response.