gtdiehl / iotawatt_ha

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

Sensor state isn't updated unless the value change. #16

Closed jyavenard closed 3 years ago

jyavenard commented 3 years ago

The consequence of this problem was discussed in : https://community.home-assistant.io/t/custom-component-iotawatt-energy-monitor-integration/254110 in which I noticed an issue when calculating the integration of a power sensor in order to get the energy accumulated since the last read. I also opened that bug: https://github.com/home-assistant/core/issues/55130

What we see is that when integrating the power sensor some datas were ignored, causing the integration to return invalid result.

However, after writing a little automation writing to a file whenever a sensor was updated:

- alias: Log iota sensors
  trigger:
    - platform: state
      entity_id:
        - sensor.iotawatt_output_export
        - sensor.iotawatt_output_import
        - sensor.power_grid_export
        - sensor.power_grid_import
        - sensor.energy_grid_export
        - sensor.energy_grid_import
        - sensor.daily_exported_energy
        - sensor.daily_purchased_energy_peak
        - sensor.daily_purchased_energy_offpeak
  condition: []
  action:
    - service: notify.sensor_update
      data:
        message: "{{ now() }} {{ trigger.to_state.entity_id }} : {{ states(trigger.to_state.entity_id) }}"
  mode: queued
  max: 10

it quickly became evident on what was happening:

in there I see every 30s:

2021-08-24T13:00:58.948066+00:00 2021-08-24 23:00:58.931224+10:00 sensor.iotawatt_output_import : 956.7
2021-08-24T13:00:58.990680+00:00 2021-08-24 23:00:58.980965+10:00 sensor.power_grid_import : 956.7
2021-08-24T13:00:59.042709+00:00 2021-08-24 23:00:59.016679+10:00 sensor.energy_grid_import : 27.988
2021-08-24T13:00:59.090497+00:00 2021-08-24 23:00:59.083603+10:00 sensor.daily_purchased_energy_offpeak : 16370.645
2021-08-24T13:01:29.427267+00:00 2021-08-24 23:01:29.409896+10:00 sensor.iotawatt_output_import : 920.4
2021-08-24T13:01:29.454557+00:00 2021-08-24 23:01:29.446324+10:00 sensor.power_grid_import : 920.4
2021-08-24T13:01:29.516071+00:00 2021-08-24 23:01:29.485562+10:00 sensor.energy_grid_import : 27.996
2021-08-24T13:01:29.617179+00:00 2021-08-24 23:01:29.576890+10:00 sensor.daily_purchased_energy_offpeak : 16370.653
2021-08-24T13:01:58.775600+00:00 2021-08-24 23:01:58.767084+10:00 sensor.iotawatt_output_import : 991.8
2021-08-24T13:01:58.814723+00:00 2021-08-24 23:01:58.800374+10:00 sensor.power_grid_import : 991.8
2021-08-24T13:01:58.844214+00:00 2021-08-24 23:01:58.834699+10:00 sensor.energy_grid_import : 28.003
2021-08-24T13:01:58.874055+00:00 2021-08-24 23:01:58.864241+10:00 sensor.daily_purchased_energy_offpeak : 16370.660
2021-08-24T13:02:29.437440+00:00 2021-08-24 23:02:29.413309+10:00 sensor.iotawatt_output_import : 929.6
2021-08-24T13:02:29.518908+00:00 2021-08-24 23:02:29.472959+10:00 sensor.power_grid_import : 929.6
2021-08-24T13:02:29.584119+00:00 2021-08-24 23:02:29.577050+10:00 sensor.energy_grid_import : 28.012
2021-08-24T13:02:29.632731+00:00 2021-08-24 23:02:29.621587+10:00 sensor.daily_purchased_energy_offpeak : 16370.669
2021-08-24T13:02:58.854691+00:00 2021-08-24 23:02:58.834879+10:00 sensor.iotawatt_output_import : 957.5
2021-08-24T13:02:58.889165+00:00 2021-08-24 23:02:58.881409+10:00 sensor.power_grid_import : 957.5
2021-08-24T13:02:58.930739+00:00 2021-08-24 23:02:58.913406+10:00 sensor.energy_grid_import : 28.019
2021-08-24T13:02:58.959762+00:00 2021-08-24 23:02:58.949684+10:00 sensor.daily_purchased_energy_offpeak : 16370.676

the iota output sensor for the export output (which would be 0 at this time of night) aren't updated. As such when dealing with data points that should be: (00:00:00, 0) (00:00:30, 0) (00:01:00, 0) (00:01:30, 0) ... repeat (08:59:30, 0) (09:00:00, 188.1) (09:00:30, 205)

what the integration gets to be provided is in fact: (00:00:00, 0) (09:00:00, 188.1) (09:00:30, 205)

And so the first data entered at 9AM will be 188.1W9h = 1.692kWh instead of 188.1W30s = 188.1W*30/3600h = 0.0015675kWh

The sensors must be updated even if the value isn't changing as it otherwise can't be used with the HA integration and provide an accurate result.

jyavenard commented 3 years ago

will amend #15 instead