bramstroker / homeassistant-powercalc

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

Feature Request: Reset a group energy sensor #845

Closed ColinRobbins closed 2 years ago

ColinRobbins commented 2 years ago

To reset a group enery sensor, you have to reset each individual energy sensor that makes up the group energy sensor.

It would be good if the reset worked at the group level, and reset every sensor in the group

bramstroker commented 2 years ago

PR https://github.com/bramstroker/homeassistant-powercalc/pull/849

ColinRobbins commented 2 years ago

Thank you.

bramstroker commented 2 years ago

Has been merged into master. You can test it out by installing the master branch. Will release new beta v0.23.0-beta.3 soon which will also contain this feature.

ColinRobbins commented 2 years ago

I tried the v0.23.0-beta.3, and the integration fails to load. Had to revert. I'm using Home Assistant 2022.7.5, Frontend 20220707.1.

ColinRobbins commented 2 years ago

The error message:

This error originated from a custom integration.

Logger: homeassistant.loader
Source: custom_components/powercalc/sensors/abstract.py:21
Integration: powercalc (documentation, issues)
First occurred: 08:11:28 (8 occurrences)
Last logged: 08:11:28

Unexpected exception importing component custom_components.powercalc
Unexpected exception importing platform custom_components.powercalc.sensor
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/loader.py", line 599, in get_component
    cache[self.domain] = importlib.import_module(self.pkg_path)
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/homeassistant/.homeassistant/custom_components/powercalc/__init__.py", line 70, in <module>
    from .sensors.group import create_group_sensors
  File "/home/homeassistant/.homeassistant/custom_components/powercalc/sensors/group.py", line 34, in <module>
    from custom_components.powercalc.sensors.energy import EnergySensor, RealEnergySensor
  File "/home/homeassistant/.homeassistant/custom_components/powercalc/sensors/energy.py", line 31, in <module>
    from custom_components.powercalc.sensors.power import PowerSensor, RealPowerSensor
  File "/home/homeassistant/.homeassistant/custom_components/powercalc/sensors/power.py", line 78, in <module>
    from .abstract import generate_power_sensor_entity_id, generate_power_sensor_name
  File "/home/homeassistant/.homeassistant/custom_components/powercalc/sensors/abstract.py", line 21, in <module>
    name: str | None,
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
bramstroker commented 2 years ago

Will issue a new beta with a fix for this shortly

bramstroker commented 2 years ago

beta v0.23.0-beta.4 just released

ColinRobbins commented 2 years ago

Same issue on Beta 4 sadly.

This error originated from a custom integration.

Logger: homeassistant.loader
Source: custom_components/powercalc/sensors/abstract.py:21
Integration: Powercalc (documentation, issues)
First occurred: 09:09:49 (8 occurrences)
Last logged: 09:09:49

Unexpected exception importing component custom_components.powercalc
Unexpected exception importing platform custom_components.powercalc.sensor
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/loader.py", line 599, in get_component
    cache[self.domain] = importlib.import_module(self.pkg_path)
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/homeassistant/.homeassistant/custom_components/powercalc/__init__.py", line 70, in <module>
    from .sensors.group import create_group_sensors
  File "/home/homeassistant/.homeassistant/custom_components/powercalc/sensors/group.py", line 38, in <module>
    from custom_components.powercalc.sensors.energy import EnergySensor, RealEnergySensor
  File "/home/homeassistant/.homeassistant/custom_components/powercalc/sensors/energy.py", line 31, in <module>
    from custom_components.powercalc.sensors.power import PowerSensor, RealPowerSensor
  File "/home/homeassistant/.homeassistant/custom_components/powercalc/sensors/power.py", line 78, in <module>
    from .abstract import generate_power_sensor_entity_id, generate_power_sensor_name
  File "/home/homeassistant/.homeassistant/custom_components/powercalc/sensors/abstract.py", line 21, in <module>
    name: str | None = None,
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
bramstroker commented 2 years ago

I am not sure yet as this same construct is used throughout the HA codebase. It should not give this error. I am also not able to reproduce this on my machine which makes solving a little harder. I'll investigate further today.

ColinRobbins commented 2 years ago

Here's my sample of config, it that helps, to see the way I have it set up...

- platform: powercalc
  create_group: Lights
  entities:
    - entity_id: light.loft_centre
    - entity_id: switch.kitchen_right
      fixed:
        power: 40
      standby_power: 0.4
    - entity_id: input_number.phantom_lights
      fixed:
        power: "{{states('input_number.phantom_lights')}}"

- platform: powercalc
  create_group: Garden
  entities:
    - entity_id: switch.z_pond
      fixed:
        power: 62
      standby_power: 0.4
    - name: cctv
      daily_fixed_energy:
        value: 5
        unit_of_measurement: W
        on_time: 24:00:00

- platform: powercalc
  create_group: Appliances
  entities:
    - name: coolers
      daily_fixed_energy:
        value: 110
        unit_of_measurement: W
        on_time: 24:00:00
    - name: washingmachine
      daily_fixed_energy:
        value: 600
        unit_of_measurement: W
        on_time: 2:00:00
    - name: dishwash
      daily_fixed_energy:
        value: 600
        unit_of_measurement: W
        on_time: 1:30:00
    - entity_id: input_number.phantom_kitchen
      fixed:
        power: "{{states('input_number.phantom_kitchen')}}"

Guessing, by could it be something to do with the daily_fixed_energy sensors, that do not all seem to create thier own energy sensors? Hence the None when looping through them?

ColinRobbins commented 2 years ago

PS - Thank you for your time and efforts on the great intergation. It saved me a considerable amount of time with creating my own set of tempate sensors.

bramstroker commented 2 years ago

Have a potential fix now, is on the master branch. Are you able to redownload with HACS and then select the master branch? When it is working for you I'll release another beta.

bramstroker commented 2 years ago

Fix should be in beta.5 btw

ColinRobbins commented 2 years ago

Yes, we are all good with Beta 5. Thank you.

ColinRobbins commented 2 years ago

However…, I now get en error when I run the reset on a group…

Logger: homeassistant.components.automation.power_reset
Source: helpers/service.py:711 
Integration: Automation (documentation, issues) 
First occurred: 12:42:59 (2 occurrences) 
Last logged: 12:42:59

Energy Sensor Reset: Error executing script. Unexpected error for call_service at pos 1: 'VirtualPowerSensor' object has no attribute 'async_reset_energy'
While executing automation automation.power_reset
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 447, in _async_step
    await getattr(self, handler)()
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/script.py", line 680, in _async_call_service_step
    await service_task
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1713, in async_call
    task.result()
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/core.py", line 1750, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/entity_platform.py", line 719, in handle_service
    await service.entity_service_call(
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/service.py", line 680, in entity_service_call
    future.result()  # pop exception if have
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/entity.py", line 930, in async_request_call
    await coro
  File "/srv/homeassistant/lib/python3.9/site-packages/homeassistant/helpers/service.py", line 711, in _handle_entity_call
    result = hass.async_run_job(partial(getattr(entity, func), **data))  # type: ignore[arg-type]
AttributeError: 'VirtualPowerSensor' object has no attribute 'async_reset_energy'
ColinRobbins commented 2 years ago

Also, I think when an energy sensor is rest, the last_reset attribute ought to be set to the current time. Otherwise, I think the energy dashboard will not recognise the reset, and show negative values (have done a reset, I am seeing negative values, and think this is the cause).

bramstroker commented 2 years ago

Will have another look tomorrow. I think you are right about last_reset. Also see https://developers.home-assistant.io/blog/page/3#state-class-total.

bramstroker commented 2 years ago

Btw the error you posted is because you are trying to reset a power sensor, not an energy sensor.

ColinRobbins commented 2 years ago

Btw the error you posted is because you are trying to reset a power sensor, not an energy sensor.

Opps. Thanks.

bramstroker commented 2 years ago

last_reset is implemented in v0.23.0-beta.6.