Skip to content

Commit e0a3d01

Browse files
mistral-vibemdeweerd
authored andcommitted
Fix #3: Preserve gas_daily_kwh sensor on reboot or no new data
When there is no new GRDF data or on reboot, the gas_daily_kwh sensor was disappearing. Now it preserves the last known value by reading it from Home Assistant and updating the sensor. This ensures the sensor remains available even when no new data is received.
1 parent aa5ac16 commit e0a3d01

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

apps/meters_to_ha/meters_to_ha.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3515,6 +3515,36 @@ def update_grdf_device(self, json_file):
35153515
update_state_file(
35163516
self.configuration[STATE_FILE], {"grdf": entity_data}
35173517
)
3518+
3519+
# Preserve gas_daily_kwh sensor on reboot or no new data (issue #3)
3520+
# Try to get the last known value from HA
3521+
for daily_sensor in (
3522+
sensor_name_daily_pce_kwh,
3523+
sensor_name_daily_generic_kwh,
3524+
):
3525+
try:
3526+
response = self.open_url(HA_API_SENSOR_FORMAT % (daily_sensor,))
3527+
if isinstance(response, dict) and "state" in response:
3528+
# Preserve the last known value
3529+
last_daily_kwh = response["state"]
3530+
last_attributes = response.get("attributes", {})
3531+
3532+
# Update the sensor to preserve its value
3533+
preserve_data = {
3534+
"state": last_daily_kwh,
3535+
"attributes": {
3536+
**last_attributes,
3537+
"last_check": now_isostr,
3538+
},
3539+
}
3540+
r = self.open_url(
3541+
HA_API_SENSOR_FORMAT % (daily_sensor,), preserve_data
3542+
)
3543+
self.mylog(f"Preserved {daily_sensor}: {last_daily_kwh}")
3544+
break
3545+
except RuntimeError:
3546+
# Sensor doesn't exist yet or is unavailable
3547+
pass
35183548
else:
35193549
self.mylog(
35203550
f" update value is {date_time.isoformat()}:"

0 commit comments

Comments
 (0)