I have 2x HomeWizard batteries, integrated with Home Assistant (2025.4.2).
Looking to add a “Maximum State of Charge % Today” sensor.
The sensor must reset to 0% at midnight
The sensor must maintain the maximum percentage,
as well as the time that the max was last updated
Statistics sensors do not reset at midnight, and after a lot of searching it seems that Home Assistant does not have this capability. Template Sensors seem to be the way to go, so I copied an example from this forum. I had to modify it due to (new) syntax changes.
My idea is to add a time trigger that fires at 00:00, resetting the value to 0%. The sensor should also evaluate and update whenever the input sensors change.
The code below does not work as expected; I added additional numeric_state triggers (above -1) to hopefully update the value but that does not work either.
The inputs are sensor.homewizard_accu_1_laadpercentage and sensor.homewizard_accu_2_laadpercentage.
The derived “max value today” sensors are “HomeWizard Accu 1 max lading vandaag” and “HomeWizard Accu 2 max lading vandaag”
I was hoping this would be easy, but it seems I need some help
Does anyone have suggestions on how to get this working? Thanks!
You don’t have the correct triggers to update it throughout the day… The templates of trigger-based Template sensors are only rendered following a trigger, unlike state-based Template sensor which update on state changes of the entities they reference. For most cases like what you have presented that means you’ll want to include State triggers.
Regarding the Numeric state triggers, unless your charge goes below -1 at some point, and then raise above -1, those triggers aren’t going to do anything. Numeric triggers only fire when the defined threshold is crossed, they do not continue to fire while the value remains past the threshold.
Also, make sure you are using the correct trigger platform… in the example you are using a time_pattern trigger, but the state templates check for a time trigger. Either one will work as the trigger for midnight, but your state will be incorrect if what you check isn’t what you used.
Thank you! Perhaps I should include a trigger that fires when the state of the entity is larger than the current maximum - I was indeed assuming that the trigger would fire on every entity change, on the condition that the value was larger than -1
Thanks - I’ve modified the code to use a state trigger like you suggested. It’s dark and the batteries are empty, so I’ll have to wait until tomorrow to see if it worked
Update: nope, it still doesn’t work ;-( The sensors show as ‘Unavailable’ which I did not expect given the state trigger ‘not_to’ clause, the availability ‘is_number’ and various float(0) defaults.
I’ve fixed the ‘time’ vs. ‘time_pattern’ issue and reviewed the logic, but I’m still overlooking something here.
There’s a known logic issue in the code below: at midnight, the max should not be reset to zero, but to the current charge percentage. I left that ‘bug’ in for now, focusing on getting the basics to work first.
I wish there were a way to single-step through the code and see if triggers actually fire - that would help me a lot in debugging.
Following up to myself - I know this might be controversial, but I asked CoPilot to help me understand the code. It stated something that indeed bothered me: I could not see how the trigger would ‘know’ what sensor to reset:
To properly link the reset trigger to the max sensor, you need to use an automation that resets the max sensor’s value at midnight. Home Assistant’s template sensors do not inherently support state resets based on triggers, so you need to handle this logic externally.
I will move the trigger code from the template sensor to an automation, see if that helps.
Home Assistant’s Trigger-based Template Sensor definitely supports “state resets based on triggers”.
No.
In fact, Home Assistant doesn’t have a native action to set a sensor entity’s state value. Ask Copilot what action it suggests its proposed automation will use to “resets the max sensor’s value at midnight”.
Now you know why it’s “controversial”. It’s a source of convincing but faulty Home Assistant advice.
I agree that CoPilot (or any LLM) output must be carefully reviewed. However, the fact remains that the state was definitely not being reset using the ‘supported’ approach with a template trigger/sensor
Hence my search for another approach. I do hope to return to the ‘supported’ way once I have the MVP running. The automation looks like this, using ‘homeassistant.update_entity’ which seems to actually exist - of course untested until tomorrow rolls around:
alias: Helper - Reset at midnight
description: Reset input helpers, sensors etc. at midnight
triggers:
- at: "00:00:00"
trigger: time
conditions: []
actions:
- target:
entity_id: sensor.homewizard_accu_1_max_laadpercentage
action: homeassistant.update_entity
data: {}
- target:
entity_id: sensor.homewizard_accu_2_max_laadpercentage
action: homeassistant.update_entity
data: {}
mode: single
All these examples rely on a Trigger-based Template Sensor’s support for triggers that can initiate actions to modify its value(s). Not, like CoPilot suggested, via an automation.
Regarding Didgeridrew’s example, its behavior is entirely dependent on the behavior of the two sensor entities it’s monitoring.
How often do their values change, are they normally numeric, have they recently reported non-numeric values and what were they? (none, unavailable, unknown, something else?)
Hi, thank you for your patience - I’m currently on mobile, will definitely try your suggestions once I’m able. The values are always (?) numeric 0-100 percentages.
You can create an automation that uses the same State Triggers as the ones in your Trigger-based Template Sensor. The automation’s actions can simply use notify.persistent_notification to report the value of whichever one of the two sensor’s triggered it (plus any other information you want to report). It will provide insight into the data being received by the Trigger-based Template Sensor.