Many threads on this subject, but I don’t find a satisfying answer.
For background; I have a ‘virtual’ battery to store access energy from the solar panels. I would like to find out what the maximum energy stored in that battery was for a calendar year.
I’m using the statistics - max_value for that now. But I noticed that the value dropped at some point. I believe this is because of the number of samples retained or something like that. But of course I do not need to keep all samples over the course of one year to determine the maximum value the way the statistics function works.
I just need to store the current max value and compare that to the current actual value to update the max value. For that I need the max value to be stored in a way that survives reboots. Many threads about that as well, but again I didn’t find a working solution.
I think I have this working now. I abuse the ‘input_number’ variable and ‘set_value’ service.
It’s a three step setup.
Define the ‘input_number’ variable
I have below code in my configuration.yaml file. The ‘min’, ‘max’, and ‘step’ parameters are mandatory and for the slider that will not be used. Just to make sure I set them to reasonable values for my use case. ‘input_number.battery_peak_energy’ will hold the maximum value I want to store to survive a reboot.
Define an automation to update the ‘input_number’ variable
I have created the automation below to update the ‘input_number’. It was counter intuitive that I had to create an automation, but it is pretty clean this way. The automation is triggered when the actual value exceeds the stored maximum value and then updates the stored maximum value with the actual value. Part of this automation could not be defined on the UI, I used fixed numbers for the ‘above’ value (input_number.battery_peak_energy) and the ‘set_value’ value ("{{ states(‘sensor.energy_stored_meter’) }}") and then edited the automations.yaml file.
- id: '1644321547640'
alias: Store battery peak energy
description: Calculate and store battery peak energy in input_value to survive a
reboot
trigger:
- platform: numeric_state
entity_id: sensor.energy_stored_meter
above: input_number.battery_peak_energy
condition: []
action:
- service: input_number.set_value
data:
value: "{{ states('sensor.energy_stored_meter') }}"
target:
entity_id: input_number.battery_peak_energy
mode: single
Define a sensor to display the ‘input_number’ variable
If you display the ‘input_number’ on the lovelace dashboard you get the slider to change the value. That’s not what I want here obviously. So I defined a sensor as below in the configuration.yaml file to create a ‘sensor.battery_peak_energy’ to display.
It triggers the moment the value of sensor.energy_stored_meter increases above the threshold value of input_number.battery_peak_energy.
The trigger is now “set”. If the value of sensor.energy_stored_meter continues to increase, it will not trigger again.
To “reset” the trigger, the value of sensor.energy_stored_meter must first decrease below the value of input_number.battery_peak_energy. After that happens, it will trigger again whenever the value of sensor.energy_stored_meter is above the value of `input_number.battery_peak_energy
I don’t think this triggering behavior is suitable for your application.
I suggest you use a State Trigger with a Numeric State Condition that checks if the new value of sensor.energy_stored_meter exceeds the value of input_number.battery_peak_energy.
However, is it enough that input_number.battery_peak_energy and sensor.energy_stored_meter are equal (they will be due to the action) to trigger again when sensor.energy_stored_meter keeps rising?
I now see what you are saying too; the threshold value is increased after each time the automation is triggered. It might still need the sensor’s value to be below the threshold for it to reset, not merely equal. You’ll need to test it.
Anyway, the trigger does not seem to work at all. So I will have to use what you suggest in your last paragraph. But I have no idea on how to do that …
I believe you are trying to only find the max value for a single sensor over a year period, but I would be careful of causing HA to fire off a query against it’s state table and cause performance issues. At a base level success may depend on the database software you are using for HA recorder and your overall HA hardware platform. I don’t believe there is a way to force a query path for something like this in HA, and a result you might end up with a very slow and heavy on HA execution. At the core, I am not sure HA’s raw state and event storage model is well designed for ‘long term’ value storage and retrieval. I am thinking this is why the newer statistical summary work and tables are appearing.
Unfortunately, to get actionable predictive data for energy and environment, you need a couple years of historical data on your home.
I know you are looking for a easy way to accomplish your goal, but you might want to consider creating the sensor outside HA’s space and use one of the external tools, such as appdaemon.
Number helpers (input_number entities), number and sensor entities that contain a numeric value, can be used in the above and below thresholds, making the trigger more dynamic
It’s not working the way you expect probably because of the Numeric State Trigger’s behavior I explained above.
Try this:
- id: '1644321547640'
alias: Store battery peak energy
description: Calculate and store battery peak energy in input_value to survive a
reboot
trigger:
- platform: state
entity_id: sensor.energy_stored_meter
condition:
- condition: numeric_state
entity_id: sensor.energy_stored_meter
above: input_number.battery_peak_energy
action:
- service: input_number.set_value
data:
value: "{{ trigger.to_state.state | float(0) | round(3) }}"
target:
entity_id: input_number.battery_peak_energy
mode: single
I dont know if it would be too much work but I use IFTTT integration to log the level of my oil tank along with the date and outside temperature to a google sheets spread sheet. This updates once a day and allows me to look at the data long term in google sheets and manipulate the data as needed (through a graph etc)
Don’t get it. If he defines a trigger as above, which will trigger perhaps 1 to 2 times per month, where should be the problem? Every trigger in HA works this way: something happens (here x above y) and something will happen (here: store the new, higher value).
- id: '1644321547640'
alias: Store battery peak energy
description: Calculate and store battery peak energy in input_value to survive a
reboot
trigger:
- platform: state
entity_id: sensor.energy_stored_meter
condition: []
action:
- service: input_number.set_value
data:
value: "{{ [ states('sensor.energy_stored_meter') | float(0), states('input_number.battery_peak_energy') | float(0) ] | max }}"
target:
entity_id: input_number.battery_peak_energy
mode: single
Of course the automation is now triggered much, much more often.