MQTT messages ignored...sometimes

Hi everybody!
Lately I’m having strange problems: HA doesn’t react on MQTT mesages (and so it doesn’t see some devices) for some times, then it does again, randomly.
For example I’ve this automation:

alias: pulsante 6x cucina luce cucina
description: ""
triggers:
  - domain: mqtt
    device_id: 787de1b1a9130a4273130742fde97ba5
    type: action
    subtype: button_1_single
    trigger: device
conditions: []
actions:
  - action: light.toggle
    metadata: {}
    data: {}
    target:
      entity_id: light.piantana_cucina
mode: single

That works when it receives:

{"action":"button_6_single","battery":100,"linkquality":69,"operation_mode":null,"power_outage_count":271,"voltage":3156}

but this automation:

alias: pulsante 6x cucina luce cucina
description: ""
triggers:
  - domain: mqtt
    device_id: 787de1b1a9130a4273130742fde97ba5
    type: action
    subtype: button_1_single
    trigger: device
conditions: []
actions:
  - action: light.toggle
    metadata: {}
    data: {}
    target:
      entity_id: light.piantana_cucina
mode: single

doesn’t work when it receives this message:

{"action":"button_1_single","battery":100,"linkquality":63,"operation_mode":null,"power_outage_count":271,"voltage":3156}

I mean that it’s not fired
How’s that possible?
And it doesn’t work now, tonight or tomorrow it’ll may do.
This happens with all the other MQTT devices.
I’ve subscribed to some topics in the HA terminal and I see all the messages, but HA seems to ignore them.

What’s going on?
Thanks!

Try using an MQTT trigger instead of a device trigger, Automation Trigger - Home Assistant

I could try, thanks for the idea, but this setup has worked for months and I’d like to understand what’s going on, especially since the problem comes and goes.

Just avoid device triggers/conditions and actions. They aren’t good. Why and how to avoid device_ids in automations and scripts

Thanks, I’ll switch to using entities.

While I’ll edit my automations, the problem isn’t here because the devices also becomes unavailable:


What the heck is happening here?

Download MQTT Explorer (free) and check out what is being published to your topics.

Are they discovered devices or did you add them manually with YAML?

Good point, they are all discovered, the ones I’ve added manually in the yaml are still working.
Those discovered are all Zigbee (I use Zigbee2MQTT).

Are they still marked as available in Z2M?

Yes, and I can still control them via the Zigbee2MQTT web UI and via MQTT.
They still publish their status.

Do they re-appear if you restart Home Assistant?

Nope.
But sometimes they do and they work, just out of the blue, without me doing anything!
Should I call the ghost busters???

Check your Z2M addon logs.

I don’t have a Z2M addon, I only have an MQTT integration.
The Z2M is in a separate container and from there everything works fine, I’m monitoring all the topics and messages with MQTT Explorer and the status updates are published normally and if I publish something, the devices reacts as expected publishing updates.

P.s. Thanks for the time you’re spending trying to help me!