snarky-snark / home-assistant-variables

A custom Home Assistant component for declaring and setting generic variable entities dynamically.
Apache License 2.0
278 stars 16 forks source link

support HA long term statistics #62

Closed matzzzz closed 6 months ago

matzzzz commented 2 years ago

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

snarky-snark commented 2 years 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.

ehendrix23 commented 2 years ago

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:

  1. Update the domain from var to sensor. It would break existing configurations however as now any entity defined with var would become a sensor.
  2. Copy recorder.py from components/sensor and update so it looks for var domain instead of sensor. Would not break anything existing however future changes done in components/sensor/recorder.py would not be reflected in here.
ehendrix23 commented 2 years ago

I looked further into this. I don't think that either option is clean.

  1. Doing it for all would break anything existing people have setup for this. Only option would be is to add a specific parameter to make it as a sensor instead so people know when enabled it will be a sensor entity and not a var entity but I don't think that would be real clean.
  2. Would need to copy but it also means that any changes to long term statistics would not be reflected in here.

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.

RoboMagus commented 2 years ago

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.

snarky-snark commented 6 months ago

It's a good idea, but as discussed it does not look feasible.