Skip to content

Question: MQTT client doesn't re-publish state after broker restart #1141

@michaelmedellin

Description

@michaelmedellin

I noticed that when my MQTT broker restarts, nodejs-poolController reconnects successfully but doesn't re-publish the equipment state. Home Assistant shows all pool entities as "unavailable" until I resave the MQTT configuration or restart the pool controller.

Looking at mqttInterface.ts, I think I found the issue. The sentInitialMessages flag is set to true after the first connect and controls whether emitAllEquipmentChanges() runs. But the flag is never reset on disconnect or offline events, so when the broker restarts and the client reconnects, emitAllEquipmentChanges() doesn't get called again.

I have a fork with the following offline event handler working successfully:

this.client.on('offline', () => {
    logger.info(`MQTT client offline ${this.cfg.name}`);
    this.sentInitialMessages = false;
});

This way when the broker comes back and the client reconnects, it re-publishes all equipment state. Happy to submit a PR if this approach makes sense.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions