bramstroker / homeassistant-powercalc

Custom component to calculate estimated power consumption of lights and other appliances
MIT License
1.02k stars 263 forks source link

Support for multiple-outlet smart outlets and smart strips #2404

Closed codicusmaximus closed 3 months ago

codicusmaximus commented 3 months ago

Checklist

Is your feature request related to a problem? Please describe.

It creates a lot of work and entity/device count bloat having to add a separate PowerCalc instance for each outlet in a smart power strip (such as the TP-Link HS300).

Describe the solution you'd like

The HS300 for example has 6 outlets each one with standby consumption of 0.25W and switched-on consumption of 0.687W (measured with a clamp meter and a 10X probe with 4 units in parallel for better precision). Having to add 6 Power Calc instances and then a group on top of that has been very cumbersome considering my house has about 20 of these strips. I am starting to replace outlets in the wall with 2-plug (top/bottom) smart outlets which present the same problem.

This could be as simple as a "smart_switch_multiswitch" new device type with a base consumption setting (standby_power) for the whole device (1.5W for the HS300) and a "output relay consumption" value which expresses how much energy each output relay in the device consumes (0.437W for the HS300). Then a list of switch entities is configured (kind of like in groups) where each of the switch entities adds that much power consumption.

Describe alternatives you've considered

N/A

Additional context

How it works currently also creates issues with power sensor and energy sensor naming when I tried to make my own library entry since the naming expects to use the device's name for the {} substitution instead of the individual switch's name being attached to so on top of manually adding all of these you have to manually mess with the names.

bramstroker commented 3 months ago

Cool device btw, didn't know it existed. Could you show all the entities available for this TP-Link HS300 device? Screenshot of the possible entities, and attributes of developer tools for a switch entity?

Need to do a bit of brainstorming how to integrate this nicely. I see added value in this so will consider.

codicusmaximus commented 3 months ago

Absolutely! More than happy to assist in any way I can. Thanks for the consideration

Here are the screenshots of one I set up just yesterday and haven't named the outlets yet: hs300 hs300-attributes-1 hs300-attributes

bramstroker commented 3 months ago

Thanks. Already did a lot of work with #2409.

The idea is you self multi_switch from calculation strategy dropdown. Which allows you to select the individual outlet switch entities.

Screenshot 2024-07-26 at 14 54 53

Currently working on discovery flow, than it won't ask for power on and power off as that is already provided by the power profile. The challenge left here is to make sure only one discovery flow is provided per unique HS330 device, so it won't discover 6.

codicusmaximus commented 3 months ago

That looks perfect!

I am in the process of validating my power usage measurements and taking pictures of them 👌

codicusmaximus commented 3 months ago

Setup:

1.35W idle all relays off (~.225W per outlet) 1.82W 1 relay on (+0.47W) 2.28W 2 relay on (+0.46W) 2.81W 3 relay on (+0.53W) 3.28W 4 relay on (+0.47W) 3.78W 5 relay on (+0.50W) 4.34W 6 relay on (+0.56W)

final findings: 0.225 W per switch/outlet (off) 0.725 W per switch/outlet (on)

Photos_riQewTd07N

As mentioned, real usage is probably a bit lower, i get 17.36W from the clamp meter calculation (which I don't think takes power factor into consideration) and 15 and sometimes 16W on the display of the kill-a-watt for the same measurement. But for tracking smart home device usage/overhead I think its better to err on a side of a bit too high since if the voltage goes a bit higher the relays will use a little more power as well (my 17.36W calculation assumes worst case voltage of 125V and the Kill-a-watt is using my actual of 120V currently)

I wish it provided a power sensor for the USB ports tho lol -- I actively avoid using them since I can't track them. For a fixed load I will occasionally use the USB port and add a manual fixed/static power calc for it :)

I also wish their similar 3-outlet mini strip had power monitoring but for some reason they chose not to include power monitoring on the 3 outlet version :(

bramstroker commented 3 months ago

Thanks for the detailed write up about the measurements.

I have just finished the development and merged to master branch. Are you able to install the latest master (you can using HACS), and give it a test?

codicusmaximus commented 3 months ago

Absolutely -- giving it a shot now

codicusmaximus commented 3 months ago

Hmm oddly enough when I click redownload in HACS and choose "master" and then inspect the directory it doesn't have the newer/modified files in there. Should I just download master manually and put the file in myself over FTP or SMB or is there something you need to do on your end to make the 'master' thing work through HACS (update tag maybe? since I think maybe it pulls from tags not branches?). Maybe I'm just being dumb, sorry if I am.

bramstroker commented 3 months ago

Ah yes, I remember from few months ago that downloading master from HACS was not working anymore :-( Yes you can just download manually, using this link: https://github.com/bramstroker/homeassistant-powercalc/archive/refs/heads/master.zip

You can put the contents of custom_components/powercalc into config/custom_components/powercalc in your HA installation.

codicusmaximus commented 3 months ago

Hmm, odd, my multi-switch config page looks like this chrome_9u332ewiv5

Also, the auto discovery shows 6 copies per device (one for each of the switches), and when you add them it creates separate tracker instances. chrome_8l8SR52S7D chrome_XkZdR49lr8

I also keep getting this when trying a second time to add something after canceling out or getting a 'already configured' message: chrome_OywOSmFpPJ

codicusmaximus commented 3 months ago

Turns out this was my fault -- caused by a left over model file from when I was manually adding these before. I deleted that and am re-testing. Only seeing 1 instance now per strip by autoconfig.

codicusmaximus commented 3 months ago

I got this trying to add one from the auto config:

2024-07-26 16:08:58.702 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
File "/usr/local/lib/python3.12/site-packages/aiohttp/web_protocol.py", line 452, in _handle_request
resp = await request_handler(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/aiohttp/web_app.py", line 543, in _handle
resp = await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/aiohttp/web_middlewares.py", line 114, in impl
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 92, in security_filter_middleware
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/http/forwarded.py", line 83, in forwarded_middleware
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 26, in request_context_middleware
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 85, in ban_middleware
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 242, in auth_middleware
return await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/http/headers.py", line 32, in headers_middleware
response = await handler(request)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/http.py", line 73, in handle
result = await handler(request, **request.match_info)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/http/decorators.py", line 81, in with_admin
return await func(self, request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/config/config_entries.py", line 222, in post
return await super().post(request, flow_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/http/data_validator.py", line 70, in wrapper
return await method(view, request, data, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/data_entry_flow.py", line 122, in post
result = await self._flow_mgr.async_configure(flow_id, data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 368, in async_configure
result = await self._async_configure(flow_id, user_input)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 414, in _async_configure
result = await self._async_handle_step(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 517, in _async_handle_step
result: _FlowResultT = await getattr(flow, method)(user_input)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/powercalc/config_flow.py", line 1035, in async_step_library
return await self.async_step_post_library(user_input)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/powercalc/config_flow.py", line 1155, in async_step_post_library
return await self.async_step_multi_switch()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/powercalc/config_flow.py", line 973, in async_step_multi_switch
data_schema=self.create_schema_multi_switch(),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/powercalc/config_flow.py", line 506, in create_schema_multi_switch
del schema.schema[CONF_POWER]
~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: 'power'

Along with this: chrome_QPcBGq5ch4

codicusmaximus commented 3 months ago

OK, oddly enough, I erased my entire config to start clean, and now they add correctly when added from the auto-discover. Seems to work correctly. I will continue to explore.

hmm, double strike that, it seems like I got one added but then the others give the same error

codicusmaximus commented 3 months ago

OK, so, it looks like I can add 1 device, and after that I get that unknown error every time. I have to restart home assistant which then lets me add 1 device and rinse and repeat. Very odd.

Also, just to confirm -- after clicking the blue "configure" button, it still makes me manually add the list of switch entities. Is this expected / just a limitation of how it works or is it just not working correctly?

bramstroker commented 3 months ago

Hmm, odd, my multi-switch config page looks like this

Probably translations not loaded correctly. Which language have you set HA in? When english clear browser cache.

OK, so, it looks like I can add 1 device, and after that I get that unknown error every time. I have to restart home assistant which then lets me add 1 device and rinse and repeat. Very odd.

I will look where this error comes from and if I can reproduce.

Also, just to confirm -- after clicking the blue "configure" button, it still makes me manually add the list of switch entities. Is this expected / just a limitation of how it works or is it just not working correctly?

Yes that's how it works for current version. When I would automatically add all switch entities from the same device, we need to make sure there are no other switch entities which are not actually the relays. Might be a future development.

bramstroker commented 3 months ago

I have found the issue. Will be fixed with #2412, which I will merge to master in a few minutes.

bramstroker commented 3 months ago

You can install v1.14.0-beta.0, which also contains fix for the multi switch flow. It also brings a cleaned up option flow, where options are split into several pages.

codicusmaximus commented 3 months ago

Awesome! Giving it a spin now!

codicusmaximus commented 3 months ago

Seems to work great! Amazing work!

bramstroker commented 3 months ago

Awesome. thanks for testing. Closing the issue.