Zigbee2mqtt retain not working

Hi all,

I’m using hassio in an Intel Nuc (docker) and I’ve added zigbee2mqtt add-on and successfully paired a Xiaomi Aqara sensor.
The problem comes when I restart home assistant, the value of the temperature/humidity/preasure/battery is unknown until a new message from the sensor is received or I press the sensor’s button.
I added in devices.yaml the option “retain: true” but it seems that does not work

'0x00158dxxxxxxxxxx':
  retain: true
  friendly_name: my_sensor
  debounce: 1

Does anybody know how to fix this issue?
Thanks,
Oscar

I’ve observed that if I use the mqtt service and I listen for the topic I can see that (pressing the sensor’s button or when it is updated automatically):

image

this tells me that retain: true is not working…

I think I have found a workaround for that.
Restarting zigbee2mqtt add-on when home assistant starts you can get the latest values stored on share/zigbee2mqtt/state.json. Keep in mind that this file is updated every 5 min so you may lose the latest value if the reset is produced before state.json is stored.
The automation is:

alias: Init stuff
trigger:
  platform: homeassistant
  event: start
action:
  - service: hassio.addon_restart
    data:
      addon: "7ad98f9c_zigbee2mqtt"

Did you read

alias: Init stuff
trigger:
  platform: homeassistant
  event: start
action:
  - data:
      topic: zigbee2mqtt/[friendly_name]
    service: mqtt.publish

1 Like

Thanks for the suggestion but it’s not working to me :worried:

I finally found the solution reading the zigbee2mqtt documentation :sweat_smile:
The key is here:

mqtt:
  discovery: true
  broker: [YOUR MQTT BROKER]  # Remove if you want to use builtin-in MQTT broker
  birth_message:
    topic: 'hass/status'
    payload: 'online'
  will_message:
    topic: 'hass/status'
    payload: 'offline'

the birth/will message allows that:

zigbee2mqtt resend the cached values when Home Assistant restarts

Additionally, I’ve added to the addon configuration.yaml:

device_options:
  retain: true

Now, after each home assistant reset (without reseting the addon) I have on hassio the latest sensor data received.

1 Like

Oh, I thought everyone had these lines configured. It’s in the first pages of the zigbee2mqtt documentation.

Sometimes I want to go faster than I should… Anyway, thanks for you help

default topic of mqtt broker has changed to homeassistant/status is there anyway to change the topic for zigbee2mqtt to listen to that default topic instead off hass/status ?

I think it is changed in zigbee2mqtt 1.14.2