gtdiehl / iotawatt_ha

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

wh issue when using Iotawatt with calculations #13

Open Glazzanz opened 2 years ago

Glazzanz commented 2 years ago

I am using calculated values to ensure that my input is never reporting as exporting, and exporting is never reporting as importing. However this isnt working with the integration. It appears the api from iotawatt is the root cause - but they have no plans to resolve.

https://github.com/boblemaire/IoTaWatt/issues/303

gtdiehl commented 2 years ago

In the post that is linked it mentions this query does not work http://iotawatt/query?select=[net.wh,grid_consumption.wh,grid_return.wh]&begin=d&end=s&group=all

Unfortunately changing the parameter of group to auto or some other value the IoTaWatt backend returns an array with multiple elements. Each element for the time period of the grouping from the beginning of the year. Currently, the API is written to expect an array with a single element.

This can not be resolved without a large code change. Would take some time to resolve this one.

jyavenard commented 2 years ago

Querying a Wh Iotawatt output will not work for HA and the energy component. I posted a way to work around that here: https://community.home-assistant.io/t/custom-component-iotawatt-energy-monitor-integration/254110/91?u=jyavenard

The explanation on why you can't query Wh that needs to be calculated by summing the calculations is in the thread above.

gtdiehl commented 2 years ago

@jyavenard Would the only code change (probably on the IoTawatt Home Assistant integration code side) that would need to be made is to change the negative values to positive and use the Riemann sum?

jyavenard commented 2 years ago

@jyavenard Would the only code change (probably on the IoTawatt Home Assistant integration code side) that would need to be made is to change the negative values to positive and use the Riemann sum?

Everything else can be done in HA yes.

So if you simply want to integrate the result you would add the following integration sensor:

- platform: integration
  source: sensor.iotawatt_output_export
  name: energy_grid_export_wh
  method: right

It is better to calculate the absolute value after the integration, but care should be taken to include the attributes required by the energy screen.

If you want to calculate the absolute value before the integration you need something like:

- platform: template
  sensors:
    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() }}"

You then use the sensor.power_grid_export with the integration.

The attribute_templates is important, because HA by default doesn't call the entity's watchers if the value hasn't changed, and the integration needs to know of all values, not just when it changes. Adding a last_updated attribute gets around the problem.

I automated the process above and automatically create the integration sensor with this commit: https://github.com/jyavenard/iotawatt_ha/commit/fca1f93fd13d1f0095ed1a406e4ef2717bfa55cb

I believe it makes for more user friendly approach.

What I don't like with the pure integration approach is that if you lose connectivity with the iotawatt (which somehow happens rather regularly at my place), then no energy is recorded during that time.