core: MQTT sensor unavailable every "expire_after" seconds, no matter if it was updated, since last update

The problem

I have a few MQTT temperature sensor configured with expire_after.

An example is a sensor for my freezer:

  - platform: mqtt
    name: "Freezer"
    state_topic: "sensors/FRIDGE/temperature"
    unit_of_measurement: '°C'
    device_class: temperature
    expire_after: 300

I have various automations connected to this sensor in order to know if the temperature of my frozen food is ok.

Since 2022.0 I’ve been getting bombarded by notification that the “temperature is now ok!”. This is from an automation trigger that checks if the freezer is back to at least less than -18 °C.

It seems for a split second the sensor goes into unavailable state, so when the value is then populated correctly, the automation is triggered.

I checked with MQTT explorer that the value is always updated well within the 300 seconds, and that is valid.

The only thing I noted (but it was the same before upgrading to 2022.0) is that sometimes the same value is published twice within 1-2 seconds (I think the wireless sensor may be sending it twice), but this has never been an issue and in any case I don’t understand why it would trigger an “unavailable” state.

I saw a couple of commits regarding MQTT so maybe something changed, but I can’t figure out what exactly.

What version of Home Assistant Core has the issue?

2022.2.0

What was the last working version of Home Assistant Core?

2021.12.10

What type of installation are you running?

Home Assistant OS

Integration causing the issue

MQTT

Link to integration documentation on our website

https://www.home-assistant.io/integrations/mqtt/

Example YAML snippet

- platform: mqtt
    name: "Freezer"
    state_topic: "sensors/FRIDGE/temperature"
    unit_of_measurement: '°C'
    device_class: temperature
    expire_after: 300

Anything in the logs that might be useful for us?

No response

Additional information

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 45 (13 by maintainers)

Most upvoted comments

I let this run overnight, about 18 hours. I only got cancel2 messages at startup, never later. I did not see incorrect timer expirations. I did see two correct ones, when one of my sensors had a network issue (sensor on cable modem, HA on opposite side of US, so it happens), but those expirations lined up with a disconnect (mosquitto executed the lwt on loss of conenction) and it all cleared on reconnect.

Which is a long way of saying: the workaround/fix results in stable behavior for me.

Just would like to add that I’ve been impacted as well. My sensors are updated every 60 seconds, expire_after: 300 and quite often but not every time I’m getting an unavailable state for exactly one measurement. With removing expire_after it actually didn’t happen since the change. It still happens on the sensors with expire_after. Looking forward to get this one fixed.

Oh, sorry, I didn’t notice that 👍

It should indeed not matter if the message is duplicated.

Same problem here.