bramstroker / homeassistant-powercalc

Custom component to calculate estimated power consumption of lights and other appliances
MIT License
957 stars 252 forks source link

Energy sensors not updating #2192

Closed erkr closed 2 months ago

erkr commented 5 months ago

System Health details

Ok

Checklist

Describe the issue

I'm using the new feature to force energy calculations based on the group power. I noticed these energy sensors don't update as long the group power doesn't change. That is a know issue with riemann sensors, so I guess this group power value isn't updated by a timer (I think you are doing that for individu lights as I noticed this option: force_update_frequency)

Best Eric

Reproduction steps

This Is a config example:

enable_autodiscovery: false
create_energy_sensors: false
create_utility_meters: false

sensors:

    - create_group: woonkamer lights
      unique_id: powercalc_group_woonkamer_lights
      create_energy_sensor: true
      force_calculate_group_energy: true
      entities:
      - entity_id: light.tv_lamp
      - entity_id: light.leeslamp
      - entity_id: light.bureau_koof
      - entity_id: light.zithoek
     ...

Debug logs

Na

Diagnostics dump or YAML config

No response

bramstroker commented 5 months ago

For the individual power sensors Powercalc forces a state update to let the Riemann sensor do it's work. The group sensors doesn't however. I saw some developments at HA core where there will be a time based implementation. So I suggest to wait for that development (probably next month release), as there would probably not be a hack needed which I implemented for the virtual power sensors.

erkr commented 5 months ago

Yes the real issue is definitely in the riemann integration. And the author initially (two years ago) even didn't agree it was a bug. I'm following that timer fix for months now, but don't share your optimism when it will be in 😏 If it is a lot of work for you I agree to wait! Otherwise it would be helpful as a temporary fix

bramstroker commented 5 months ago

There is actively being worked on seeing the latest pull requests:

https://github.com/home-assistant/core/pull/110685 https://github.com/home-assistant/core/pull/113932

erkr commented 5 months ago

Yes, we are monitoring the same change 🤞

erkr commented 5 months ago

Didn't make it for 2024.5.0 😳

erkr commented 4 months ago

The Riemann pull request finally made progress 🥳

bramstroker commented 4 months ago

The Riemann pull request finally made progress 🥳

Nice!

erkr commented 3 months ago

I didn't update yet, but I guess it's in the release.

erkr commented 2 months ago

Hi @bramstroker The configuration option 'max_sub_interval' finally made it into core 2024.7🥳 Reading the power calc release notes, I didn't spot it you already use them, in particular for these energy group sensors that monitor the group power instead of summing up individual energy sensors. If you can confirm it is in, I will switch over from my manual Riemann sensors and evaluate 🤗

bramstroker commented 2 months ago

Hi @erkr. nice I did notice it, but didn't adapt the powercalc configuration yet to it.

Just made a PR which should do it. #2365

Essentially it just sets max_sub_interval to same value as Powercalc's force_update_frequency option. https://homeassistant-powercalc.readthedocs.io/en/latest/configuration/global-configuration.html

This will make both the riemann group sensor made with force_calculate_group_energy and all other riemann sensors created by powercalc set the max_sub_interval to the same value, and enabling timed based updates.

You can try it out by installing master branch.

erkr commented 2 months ago

Hi nice you are working on it. I can't install the master version for some reason. It seems that the Riemann sensors have a default for max-sub-interval , as the group energie sensors now seem to update already. In the picture below at the arrow I updated to 2024.7 image

The various light groups now have more continuous energy values per hour.

Left of the arrow, the energy values are more in big chunks. Only reported when a value was changed

erkr commented 2 months ago

I replaced my manual Riemann entities with powercalc groups. Interesting that latest powercalc version without adaptions already works in 2024.7. Some nice examples: image image

Feel free to close the issue once you read this 🥳

bramstroker commented 2 months ago

Ah cool, it's a bit strange however, because looking into the code it should only enable timed based integration when explicitely passed. https://github.com/home-assistant/core/blob/dev/homeassistant/components/integration/sensor.py#L336

erkr commented 2 months ago

I don't understand that code well enough to judge. I noticed in your latest version you set that attribute to None, could that trigger the default?

bramstroker commented 2 months ago

That code means max sub interval is set to None # disable time based integration, when passed as none or when timedelta equals 0. So I don't see how it can enable timed interval. But hey... it works ;-) I'll have a look in a few days when I have some more spare time to see why.

bramstroker commented 2 months ago

I did have a look into the rieman helper code. One other change besides forces periodic integration (on time), also the energy sensor is calculated when a state change is "reported". This means the power sensor reports a state change to the state machine. Previously this was only handled when the power sensor state actually was recorded and actually changed.

So that would be the reason it was working for you already because powercalc power sensors report there state every know and then, this triggers the integration now.

But with my change also the scenario is covered when the power sensor stays fixed but does not report frequent state.

erkr commented 2 months ago

Well that explains. I didn't know your group power sensors where reporting state as that requires some extra timer as well. Thanks for looking into this 😁