gtdiehl / iotawatt_ha

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

Energy sensors created from an IotaWatt output can yield an incorrect result #18

Closed jyavenard closed 3 years ago

jyavenard commented 3 years ago

The integration queries the energy value (in Wh) for all sensors using GET request like so:

http://IP_ADDRESS/query?select=[time.iso,Phase1.wh,Phase2.wh,Phase3.wh,Tesla1.wh,Tesla2.wh,Tesla3.wh,Pool.wh,Solar1.wh,Solar2.wh,AC1.wh,AC2.wh,AC3.wh,HotWater.wh,AC.wh,Export.wh,Import.wh,Main.wh,Tesla.wh,Total.wh,Total_Phase1.wh,Total_Phase2.wh,Total_Phase3.wh,Total_Solar.wh]&begin=y&end=s&group=all

This will ask the iotawatt for the energy used by such output since the beginning of the current year.

The following thread explains on why it will not return the expected result under some circumstances: https://community.iotawatt.com/t/wrong-values-returned-by-rest-api-explained/3239/14 and: https://community.iotawatt.com/t/solar-import-export/3175

To get around this, with the inclusion of the fix for #15 and the integration Riemann sum integration, we can calculate the energy used for a given sensor. This however is a rather non-user friendly way of going at it.

What would be nice would be for this iotawatt integration to perform such operation, generate the energy sensor and whenever a power value is read, to update the energy sensor.

Using a configuration, we could define something like:

iotawatt:
  energy_sensors:
    source: Export
    name: energy_output
    unit_prefix: k
    round: 3

similar to https://www.home-assistant.io/integrations/integration/

An issue here is that if you have multiple iotawatt devices, you can only define it for one. But that's another issue.

The sensors created that way should be directly usable by the Home Assistant energy tool.

OzGav commented 3 years ago

Why would you need this if your PR is accepted https://github.com/gtdiehl/iotawatt_ha/pull/17 ?

If the Iotawatt integrations updates regularly then won't that mean the Rieman Sum integration should work as advertised?

jyavenard commented 3 years ago

Because it's pretty painful to get the integration going ; particularly if you want to first apply a template on the value like calculating the absolute value. You need to make sure that this one too get updated every update.

So far I've used that:

- platform : template
  sensors:
    power_grid_import:
      friendly_name: "Grid Power Import"
      unit_of_measurement: W
      device_class: power
      value_template: "{{ states('sensor.iotawatt_output_import')|float|abs }}"
      attribute_templates:
        last_updated: "{{ now() }}"
    power_grid_export:
      friendly_name: "Grid Power Export"
      unit_of_measurement: W
      device_class: power
      value_template: "{{ states('sensor.iotawatt_output_export')|float|abs }}"
      attribute_templates:
        last_updated: "{{ now() }}"

- platform: integration
  source: sensor.power_grid_export
  name: energy_grid_export
  unit_prefix: k
  method: left

- platform: integration
  source: sensor.power_grid_import
  name: energy_grid_import
  unit_prefix: k
  method: left

Adding an attributes for last_updated to make sure that the integration will be called as needed.

It's certainly not user friendly and prone to errors. Having the integration do all automatically, is I believe a much better solution. And no confusing on which integration method to use etc.

And actually we would be able to undo #17 as we would perform the integrations on each data read and not when a sensor is updated; saving significant space in the database.

OzGav commented 3 years ago

OK so I guess the integration would then have to keep track of when each update occurred so it could average out the values between readings... Also another question you say at the top that the IotaWatt keeps track of readings for the current year. That would seem to indicate that there will be a problem at the year rollover when that info resets? Perhaps this intehration needs to cater for that?

jyavenard commented 3 years ago

The iotawatt doesn't keep data for just a year; it's the integration querying for the data between those dates. Rollover will be managed by the energy integration as the sensor provides a last_reset attribute.

If we implemented what I'm suggesting here, it would be reading, as now the average power value used over the last 30s. It doesn't use the energy meter generated by the iotawatt.

OzGav commented 3 years ago

I guess it depends on how people are using this integration at the moment? However since it has been requested to add this to core then I would think it would be great if it did all that was necessary to be able to add fully working sensors to the energy dashboard...

jyavenard commented 3 years ago

I've come up with a better approach that queries the iotawatt for the accrual daily figure rather than yearly. It does require modifying iotawattpy base project however. The advantage of this method is that even if connectivity to the iotawatt is lost, no data will be lost during the time there's no connection.

OzGav commented 3 years ago

Happy with whatever works! Will see what response you get from the devs of both projects!

gtdiehl commented 3 years ago

@jyavenard Can you create a PR for iotawattpy library, so we can see what changes need to be done on that side? Thanks!