Closed matzzzz closed 6 months ago
Thanks for the report. This seems like it should be easy to add. Unfortunately, I don't have availability anytime soon, but I'm happy to accept pull requests.
Been looking into this today to see if I can submit a PR for this. Adding state_class and device_class was pretty easy and got that going. But even after adding that I still was not getting any long-term statistics for it. So went for a bit more digging and the platform itself also has to have 'compile_statistics'. An example of this is in components/sensor/recorder.py.
Options to solve this would be:
I looked further into this. I don't think that either option is clean.
One option that I have been using already and in the end accomplishes the same is by creating a template sensor:
For example, a var entity like this:
var:
produced_energy:
friendly_name: "Produced Energy"
initial_value: "unknown"
restore: true
unit_of_measurement: "kWh"
tracked_entity_id: sensor.produced_energy_total_kwh
value_template: "{{ {{ int(states('sensor.produced_energy_total_kwh'), states('var.produced_energy')) }}"
And then a template entity:
template:
- platform: state
entity_id:
- var.produced_energy
- platform: event
event_type: event_template_reloaded
sensor:
- name: produced_energy
unique_id: produced_energy
unit_of_measurement: "kWh"
state_class: "total"
device_class: "energy"
state: "{{ (states('var.produced_energy')|float(0) }}"
And then sensor.produced_energy will be your long term statistic.
From what I can see your suggestion to wrap the variable in a template sensor seems to be the best fit indeed. Ever since the trigger-based template sensors have been added that is what I've been doing for all of my 'custom' LTS sensors and it works perfectly fine without too much configuration hassle to set it up.
Statistics would be nice to have if there was some form of entity base in HA to inherit from that would implement all required long term stats functionality. But unless that change is implemented to HA supporting statistics in custom components outside the sensor domain does not seem feasible.
It's a good idea, but as discussed it does not look feasible.
I just encountered that this great integration didn't support HA long-term statistics when I tried to use a var variable in a statistic diagram and the variable was not showen in the drop-down list. The HA documentation encouraged me to create this issue to draw the attention of the owner of the integration to customize it for this HA feature. So now I hope that at some point someone can improve this excellent integration with this. https://developers.home-assistant.io/docs/core/entity/sensor/#long-term-statistics