Skip to content

Persist sensor values and device config across restarts - #233

Open
meiser79 wants to merge 1 commit into
robertzaage:mainfrom
meiser79:state_cache
Open

meiser79 wants to merge 1 commit into
robertzaage:mainfrom
meiser79:state_cache

Conversation

@meiser79

Copy link
Copy Markdown
Contributor

Home Assistant entities backed by GroBro's MQTT state topic dropped to "unknown" whenever HA (or GroBro itself) restarted, most noticeably for battery sensors after the battery storage was disconnected: once the inverter/battery stops reporting bat*-keys, the published payload permanently loses those fields and no retained/future message can recover them.

  • grobro/ha/client.py: cache the last known value of every input register per device and backfill any key missing from a fresh payload before publishing, so a value is only ever replaced by a newer real reading, never by a gap. The cache is persisted to state_cache_<device_id>.json and reloaded on startup. On MQTT (re)connect, the cached payload is republished (retained) so entities don't have to wait for the next device poll either.
  • Move the device config cache (config_<device_id>.json) and the new state cache from the working directory (/app, wiped on every addon rebuild) to /config, backed by a dedicated addon_config map (read_only: false) in config.yaml instead of the previous share:rw mapping. /config is guaranteed to exist when mapped, so no os.makedirs() is needed.
  • Move DUMP_MESSAGES output from /dump (generic) / /share/GroBro/dump (HA app) to /config/dump for the same reason, and drop the now hardcoded DUMP_DIR option from config.yaml and its env var.
  • Add PUBLISH_SENSORS_RETAINED to config.yaml/translations so it's actually exposed in the HA app UI (it already existed as an env var but Supervisor never passed it through).
  • Update CONFIGURATION.md and DEVELOPMENT.md: drop DUMP_DIR from the env var reference, point DUMP_MESSAGES/config-cache docs at /config, and note that plain (non-app) Docker users need to mount a volume at /config themselves for this data to survive a restart.

Note: PUBLISH_SENSORS_RETAINED alone does not fix the battery case, since a retained message is overwritten as soon as an incomplete payload is published; the backfill above is what keeps it complete.

@criticallimit

Copy link
Copy Markdown
Collaborator

wouldn´t these changes deliver wrong values for Home assistant (energy) statistics?
As long as the value stays the same till a new value is delivered the statistic will count this over time.

@meiser79

Copy link
Copy Markdown
Contributor Author

@criticallimit Hi, valid point, thanks for highlighting!! I now excluded "total_increasing" and "total" sensors from the caching.

@criticallimit

Copy link
Copy Markdown
Collaborator

If you want you can have a look at my version: https://github.com/criticallimit/GroBro

There a few more changes to Roberts GroBro. But for you maybe most interesting is that sensors are not retained but all settings are.

My version is only based on Grobro but with many other (not obviously visible, but important) changes. You can install it beside the original GroBro and switch between both making only one addon active

@robertzaage

Copy link
Copy Markdown
Owner

If you want you can have a look at my version: https://github.com/criticallimit/GroBro

There a few more changes to Roberts GroBro. But for you maybe most interesting is that sensors are not retained but all settings are.

My version is only based on Grobro but with many other (not obviously visible, but important) changes. You can install it beside the original GroBro and switch between both making only one addon active

@criticallimit I just added you to the inner circle of collaborators who are allowed to work on this project. I really appreciate your ongoing support for GroBro and would like to invite you to implement your ideas and future improvements here directly. Of course breaking changes, which require the user to reconfigure something or would break automations after release, always need a discussion in the issue section first, so that I have the chance to highlight those in our release notes.

Home Assistant entities backed by GroBro's MQTT state topic dropped to
"unknown" whenever HA (or GroBro itself) restarted, most noticeably for
battery sensors after the battery storage was disconnected: once the
inverter/battery stops reporting bat*-keys, the published payload
permanently loses those fields and no retained/future message can
recover them.

- grobro/ha/client.py: cache the last known value of every input
  register per device and backfill any key missing from a fresh
  payload before publishing, so a value is only ever replaced by a
  newer real reading, never by a gap. The cache is persisted to
  state_cache_<device_id>.json and reloaded on startup. On MQTT
  (re)connect, the cached payload is republished (retained) so
  entities don't have to wait for the next device poll either.
  total_increasing/total registers (the kWh energy counters feeding
  the HA Energy dashboard) are excluded from this cache: HA already
  handles gaps in those correctly on its own, and caching them risked
  reinstating a stale (too high) value after a legitimate reset (e.g.
  the daily "_today" counters at midnight), which combined with
  FILTER_DATA_GLITCHES could then suppress the correct lower value as
  a false glitch.
- Move the device config cache (config_<device_id>.json) and the new
  state cache from the working directory (/app, wiped on every addon
  rebuild) to /config, backed by a dedicated addon_config map
  (read_only: false) in config.yaml instead of the previous share:rw
  mapping. /config is guaranteed to exist when mapped, so no
  os.makedirs() is needed.
- Move DUMP_MESSAGES output from /dump (generic) / /share/GroBro/dump
  (addon) to /config/dump for the same reason, and drop the now
  hardcoded DUMP_DIR option from config.yaml and its env var.
- Add PUBLISH_SENSORS_RETAINED to config.yaml/translations so it's
  actually exposed in the addon UI (it already existed as an env var
  but Supervisor never passed it through).
- Update CONFIGURATION.md and DEVELOPMENT.md: drop DUMP_DIR from the
  env var reference, point DUMP_MESSAGES/config-cache docs at /config,
  and note that plain (non-addon) Docker users need to mount a volume
  at /config themselves for this data to survive a restart.

Note: PUBLISH_SENSORS_RETAINED alone does not fix the battery case,
since a retained message is overwritten as soon as an incomplete
payload is published; the backfill above is what keeps it complete.
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.

3 participants