kuralabs / iotawatt_ha

We had to fork Home Assistant IoTaWatt integration, people is crazy
Apache License 2.0
9 stars 2 forks source link

Device polling misses midnight boundaries. Workaround casues error. #4

Open tomlut opened 3 months ago

tomlut commented 3 months ago

To get this integration to work correctly with the energy dashboard it is necessary to disable polling in the integration system options and to update the sensors manually using an automation. Otherwise the sensors do not reset exactly at midnight, creating havoc with the energy dashboard.

Untitled

- id: 35371357-1c5e-41f3-9ab8-7084865d4a66
  alias: 'Poll IoTaWatt'
  trigger:
  - platform: time_pattern
    hours: "*"
    minutes: "*"
    seconds: "/30"
  action:
  - service: homeassistant.update_entity
    entity_id:
    - sensor.downstairs_hp_power
    - sensor.downstairs_hp_energy
    - sensor.export_power
    - sensor.export_energy
    - sensor.import_energy
    - sensor.import_power
    - sensor.self_solar_power
    - sensor.self_solar_energy
    - sensor.self_total_power
    - sensor.self_total_energy
    - sensor.solar_production_power
    - sensor.solar_production_energy
    - sensor.stove_power
    - sensor.stove_energy
    - sensor.upstairs_hp_power
    - sensor.upstairs_hp_energy

While this is working well it generates an error every night at midnight:

This error originated from a custom integration.

Logger: custom_components.iotawatt.coordinator
Source: helpers/update_coordinator.py:318
integration: IoTaWatt (documentation)
First occurred: 00:00:00 (1 occurrences)
Last logged: 00:00:00

Unexpected error fetching 10.1.1.250 data: list index out of range
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 318, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/iotawatt/coordinator.py", line 76, in _async_update_data
    await self.api.update(lastUpdate=self._last_run)
  File "/usr/local/lib/python3.12/site-packages/iotawattpy/iotawatt.py", line 76, in update
    await self._refreshSensors(timespan, lastUpdate)
  File "/usr/local/lib/python3.12/site-packages/iotawattpy/iotawatt.py", line 264, in _refreshSensors
    sensor.setValue(values[0][idx + 1])
                    ~~~~~~^^^
IndexError: list index out of range
dabbler68 commented 2 days ago

Thanks Tomlut. I was having the same issue, although not every night. The automation works fine. Once the power sensors are polled HA calculates the energy usage,

tomlut commented 2 days ago

I don't use this any more. It was causing other issues. I've gone back to using the core integration and just putting up with the outputs not being shown on the device card.

dabbler68 commented 1 day ago

After working smoothly for a few hours I started getting an error message every minute: "Nothing to query, update() called too soon, must wait {timespan}"

So I disabled the automation and turned polling back on in the integration. One error around midnight seems less of an issue after that. Maybe the core integration is a better solution but I will stick with this for a while.

darek-margas commented 1 day ago

There is always an option to use REST integration, pull json and parse.