@@ -223,6 +223,7 @@ def get_last_state():
223223
224224 except (FileNotFoundError , ValueError , IndexError ):
225225 logger .error ("State file corrupted or missing. Resetting to default." )
226+ logger .error (f"State file content: { data } " ) # Debugging
226227
227228 return {
228229 "state" : "idle" ,
@@ -235,8 +236,8 @@ def get_last_state():
235236def save_last_state (state , charging_power = 0.0 , total_energy_wh = None , total_energy_wh_for_summary = None , notified = False , start_time = None ):
236237 with open (STATE_FILE , "w" ) as f :
237238 if state == "charging" :
238- f .write (f"charging:{ start_time } :{ charging_power :.2f} :{ int (notified )} :{ total_energy_wh_for_summary or 0.0 } " )
239- debug (f".. save_last_state(): charging:{ start_time } :{ charging_power :.2f} :{ int (notified )} :{ total_energy_wh_for_summary or 0.0 } " )
239+ f .write (f"charging:{ start_time if start_time is not None else 0 } :{ charging_power :.2f} :{ int (notified )} :{ total_energy_wh_for_summary or 0.0 } " )
240+ debug (f"charging:{ start_time if start_time is not None else 0 } :{ charging_power :.2f} :{ int (notified )} :{ total_energy_wh_for_summary or 0.0 } " )
240241 elif state == "idle" and total_energy_wh is not None :
241242 f .write (f"idle:{ total_energy_wh :.2f} :{ total_energy_wh_for_summary or 0.0 } :{ int (notified )} " )
242243 debug (f".. save_last_state(): idle:{ total_energy_wh :.2f} :{ total_energy_wh_for_summary or 0.0 } :{ int (notified )} " )
@@ -349,7 +350,7 @@ def main():
349350 debug (f".. get_last_state() #1 \n -- state file: \n Last State: { last_state } \n Start Time: { start_time } \n Stored Power: { stored_power } \n Total Energy for Summary: { total_energy_wh_for_summary } \n Notified: { notified } \n -- new fetch: \n Charging Rate: { charging_rate } \n Total Energy: { total_energy_wh } " )
350351
351352 # Handle cable disconnection DURING charging
352- if last_state == "charging" and total_energy_wh is None :
353+ if last_state == "charging" and ( total_energy_wh is None or charging_rate == 0 ) :
353354 send_notification (f"🔌 { timestamp } : charging interrupted - cable unplugged." )
354355
355356 # Use the last known total_energy_wh_for_summary
@@ -387,12 +388,12 @@ def main():
387388 save_last_state (new_state , charging_power = charging_rate , total_energy_wh = total_energy_wh , total_energy_wh_for_summary = total_energy_wh , notified = notified )
388389
389390 # Handle charging start
390- if last_state != "charging" and new_state == "charging" :
391+ if last_state != "charging" and new_state == "charging" and not notified :
391392 send_notification (f"⚡ { timestamp } : charging started." )
392- save_last_state (new_state , charging_power = charging_rate , total_energy_wh = total_energy_wh , total_energy_wh_for_summary = total_energy_wh_for_summary , notified = False , start_time = current_time )
393+ save_last_state (new_state , charging_power = charging_rate , total_energy_wh = total_energy_wh , total_energy_wh_for_summary = total_energy_wh_for_summary , notified = notified , start_time = current_time )
393394
394395 # notify once per session about charging rate
395- if last_state == "charging" and not notified and charging_rate > 0 :
396+ if last_state == "charging" and charging_rate > 0 and not notified :
396397 send_notification (f"⚡ { timestamp } : charging rate { charging_rate } kW" )
397398 save_last_state (new_state , charging_power = charging_rate , total_energy_wh = total_energy_wh , total_energy_wh_for_summary = total_energy_wh_for_summary , notified = True , start_time = start_time )
398399
0 commit comments