Skip to content

Re-send sensor states after Home Assistant comes back online - #698

Open
N1c084 wants to merge 1 commit into
kellerza:mainfrom
N1c084:fix/republish-states-on-ha-online
Open

N1c084 wants to merge 1 commit into
kellerza:mainfrom
N1c084:fix/republish-states-on-ha-online

Conversation

@N1c084

@N1c084 N1c084 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Home Assistant does not keep non-retained MQTT states across a restart. On homeassistant/status "online" the add-on only republishes discovery and availability, so sensors reported on a fixed schedule stay unknown until their next report (up to 5 min), and change-only sensors until their next change.

on_ha_connected now starts republish_all_states() in the background (it runs under the discovery lock): 10 s and 60 s after HA comes online, every running inverter re-sends the last published value of each non-retained sensor. A value published meanwhile is sent again after ours; a failed send is logged and the pass goes on.

Home Assistant does not keep non-retained MQTT states across a restart.
On homeassistant/status "online" the add-on only republishes discovery
and availability, so sensors reported on a fixed schedule stay unknown
until their next report (up to 5 min), and change-only sensors until
their next change.

on_ha_connected now starts republish_all_states() in the background (it
runs under the discovery lock): 10 s and 60 s after HA comes online,
every running inverter re-sends the last published value of each
non-retained sensor. A value published meanwhile is sent again after
ours; a failed send is logged and the pass goes on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kellerza

Copy link
Copy Markdown
Owner

Hi, thank you for the PR. Why 10s and 60s?

Once we get the 'online' a single publish should be more than enough

@N1c084

N1c084 commented Oct 11, 2026 via email

Copy link
Copy Markdown
Contributor Author

@kellerza

Copy link
Copy Markdown
Owner

In on_ha_connected, MQTT is available and we already received "online" (or a timeout if HA is not configured correctly)

So, we can simplify this a lot by just calling a method like this (on AInverter) from on_ha_connected

async def republish_cached_sensors(self) -> None:
    """Re-send last MQTT-published non-retained states after HA comes online."""
    states = {
        asen: asen.value
        for asen in self.ss.values()
        if asen.entity
        and not asen.retain
        and asen.value is not None
        and asen.opt.schedule.report_every > 10
    }
    if states:
        await self.publish_sensors(states=states)

No delays and competing with other schedules. This will also not resend values for sensors that get published frequently (<10s from when this runs)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants