gtdiehl / iotawatt_ha

IoTaWatt for Home Assistant
Apache License 2.0
16 stars 16 forks source link

Add energy sensors #6

Closed agners closed 3 years ago

agners commented 3 years ago

The upcoming Energy dashboard in Home Assistant needs energy sensors to work. This adds support for the energy type sensors (unit watt-hours) and makes sure all requirements are met so the entities can be used by the Energy dashboard.

Related to: https://github.com/gtdiehl/iotawattpy/pull/3

jherby2k commented 3 years ago

I'm trying out this PR with the new release, and getting an error loading sensors:

Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 382, in async_add_entities await asyncio.gather(*tasks) File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 587, in _async_add_entity await entity.add_to_platform_finish() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 711, in add_to_platform_finish self.async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 464, in async_write_ha_state self._async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 500, in _async_write_ha_state attr.update(self.state_attributes or {}) File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 144, in state_attributes if last_reset := self.last_reset: File "/config/custom_components/iotawatt/sensor.py", line 125, in last_reset last_reset = self.coordinator.data["sensors"][self._ent].getBegin() AttributeError: 'Sensor' object has no attribute 'getBegin'

jherby2k commented 3 years ago

Ah, i see this depends on unmerged changes to iotawattpy. Guess i'll have to wait.

gtdiehl commented 3 years ago

Ah, i see this depends on unmerged changes to iotawattpy. Guess i'll have to wait.

@jherby2k I've pushed an updated iotawattpy version 0.0.6 to PyPi, which has the changes made by @agners. But it also requires the manifest.json file to be updated to grab the latest from PyPi

jherby2k commented 3 years ago

Pardon the double-post - i logged an issue with the API also. I'm honestly not sure if this is an issue with the API or this PR. Want to make sure it gets visibility from the HA integrators:

I'm having a terrible time getting a "Solar Surplus" output showing up in HA.

HA seems to want a "return to grid" sensor rather than just looking at negative grid_total values (which would make more sense to me, but whatever). So i created an output called "Solar_Surplus" that looks like this:

Watts = 0 max (-1 x (Main_1 + Main_2))

(alternately, also tried this:)

Watts = 0 max (0 - main_1 - main_2)

The wattage sensor works just fine, but I get 0 kwh for the _kw version. It also appears to never refresh until an HA restart.

FYI, my Grid_Consumed output works just fine, with

Watts = 0 max (Main_1 + Main_2)

Any suggestions? On the Iotawatt itself using the Graph+ UI, i can see these guys no problem in WH over a year, month etc. Tried recreating, adding additional outputs with different names and the same query, and none of them work.

agners commented 3 years ago

The code is fetching the input/output channel names using this query: http://host.domain/query?show=series

It then essentially is just using this query to get energy: http://host.domain/query?select=[time.iso,Input_0.wh,total_power.wh]&begin=y&end=m&group=all

What is IoTaWatt returning when you use those queries (replace total_power with our output name)?

jherby2k commented 3 years ago

Oh geez,

curl "http://192.168.1.168/query?select=\[time.iso,Solar_Surplus.wh.d6\]&begin=y&end=m&group=all"

gives me

[["2021-01-01T00:00:00",0]]

so its an Iotawatt bug. Super. This is probably gonna affect everyone trying to set up the Energy dashboard in HA. Any ideas why HA doesn't just let us set a single Grid_Total instead of needing 2 separate sensors for grid input and grid output?

gtdiehl commented 3 years ago

@agners Thank you!

@jherby2k You can disregard my questions in the iotawattpy Issue

jherby2k commented 3 years ago

I posted this at https://community.iotawatt.com/t/api-bug-for-certain-output-calculations/3172, not sure how quickly this will get looked at though...

gtdiehl commented 3 years ago

Np, I'm only on my phone rn, but is the name of your sensor being returned by the first (http://host.domain/query?show=series) query end in .d6?

Edit: Nevermind I see where the d6 is coming from. I've never seen it before but after reading the Iotawatt Query doc, I see now.

The .d modifier overides the default number of decimal digits.

jherby2k commented 3 years ago

seems to add decimal places. Works for other outputs. tried with and without.

Omitting the .wh also returns 0, which is weird since the Watt sensor looks fine in HA. you must be using a different query for that

jherby2k commented 3 years ago

I submitted a freature request as well at https://community.home-assistant.io/t/electricity-grid-should-allow-a-single-grid-total/327279. If HA would just take the Grid_Total value instead of needing these “Grid consumption” and “return to grid” sensors, I wouldn't need these min/max calculations at all.

gtdiehl commented 3 years ago

@agners Thank you for updating the sensor code to work with the latest HA code! I didn't want to merge this code into the repo here as it would cause a breaking change and force others to upgrade to 2021.8. So this is what I have done so far:

EDIT: I have gotten the tests to pass and pushed the changes to test_config_flow.py under my branch noted above.

I feel the outstanding issue is #1. Probably should resolve this one before submitting the integration to the HA dev branch?

jherby2k commented 3 years ago

re: my "return to grid" calculation issue, its sort of a limitation of IoTaWatt at this point. I'm only mentioning it now because you WILL get bug reports about not being able to set up the energy dashboard properly with IoTaWatt. Grid consumption is going to be an issue as well for most people. Here's the elaborate explaination:

https://community.iotawatt.com/t/solar-import-export/3175

the solution is really to send the power data to Influxdb and then pull it back into HA as energy using a query. WHich means not using this component.

Sounds like IoTaWatt might add the ability eventually, otherwise we need HA to add my feature request above.

agners commented 3 years ago

@jherby2k keep in mind that this is the first iteration of the energy dashboard etc. Everything will get better :smile:

I don't have solar, or rather, don't have access to it since its a large multi-tenant building. For me IoTaWatt and HA dashboard works perfectly fine :sunglasses:

From the link above it sounds like it should be possible to setup those outputs, and you should get accurate data for a day or so. Since we poll IoTaWatt multiple times a day for sure, this limit should not be a problem. We only would have to change the interval down from 1 year currently to a day.

Before doing that, can you try if getting the values using the Query API with the setup as explained works?

It is not quite clear to me how the Graph+/the Query API enforces the limits overeasy is explaining, but I guess the easiest thing is to just try...

agners commented 3 years ago

Just to be clear what I meant you should do, setup this outputs:

Outputs can be specified for Import or Export only where desired:

Import = Import/Export max 0

will yield only the energy imported from the grid.

Export = (Import/Export) min 0) abs

Use d in Query API to get the per day values

curl "http://192.168.1.168/query?select=\[time.iso,Import.wh.d6,Export.wh.d6\]&begin=d&end=m&group=all"

I think you have to wait an hour or so to query the API after setting up the outputs since IoTaWatt is only accumulating energy data from the point on where the output has been defined. But I could be wrong on that.

jherby2k commented 3 years ago

I won't be able to try that for about a week as I'm away in a road trip now, but yes I will! Would be great to have an easier solution.

On Fri., Aug. 6, 2021, 11:59 a.m. Stefan Agner, @.***> wrote:

Just to be clear what I meant you should do, setup this outputs:

Outputs can be specified for Import or Export only where desired:

Import = Import/Export max 0

will yield only the energy imported from the grid.

Export = (Import/Export) min 0) abs

Use d in Query API to get the per day values

curl " http://192.168.1.168/query?select=\[time.iso,Import.wh.d6,Export.wh.d6\]&begin=d&end=m&group=all "

I think you have to wait an hour or so to query the API after setting up the outputs since IoTaWatt is only accumulating energy data from the point on where the output has been defined. But I could be wrong on that.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/gtdiehl/iotawatt_ha/pull/6#issuecomment-894458104, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACH3T44TDVJYS6FRSPUF4XDT3QWKZANCNFSM5BKGKLCA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

gtdiehl commented 3 years ago

@agners I can merge this change into main now.

I just released the current codebase as Version 0.0.3. Hopefully not a problem for people using older versions as when I publish a new version (with yours and possibly more code changes) the minimum version of HA will be specified in the hacs.json file.