home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.18k stars 30.57k forks source link

Energy component miscalculates energy cost after HA restart or Tibber integration reload #69563

Closed uphillbattle closed 2 years ago

uphillbattle commented 2 years ago

The problem

Regularly, after restarting HA or reloading the Tibber integration, there is a hiccup in the energy cost calculation in the energy component. Typically, the Energy cost calculation is way too high after a restart/reload.

Here's the setup:

The price template sensor sensor.strompris_xxx is defined as follows:

- trigger:
  - platform: state
    entity_id: sensor.nettleie_xxx
  - platform: state
    entity_id: sensor.electricity_price_xxx
  sensor:
  - name: Strømpris xxx
    unique_id: strompris_xxx
    state: >
      {% if (states('sensor.nettleie_xxx') is not none) and
            (states('sensor.electricity_price_xxx') is not none) %}
        {% if (states('sensor.nettleie_xxx') | float(0.0) > 0.0) and
              (states('sensor.electricity_price_xxx') | float(0.0) > 0.0) %}
          {{ (states('sensor.nettleie_xxx') | float(0.0) + 
              states('sensor.electricity_price_xxx') | float(0.0)) | round(4) }}
        {% endif %}
      {% endif %}
    unit_of_measurement: NOK/kWh
    icon: mdi:currency-usd

The template sensor has a couple of if checks before actually setting the sensor - to avoid "transients" due to the source sensors taking a bit of time before getting their values after the restart. The sensor does, however, seem to be undefined through the restart (not easy to see, but there is a 37-seconds long "gap" in the curve from 10:51:02 to 10:51:39): image

In the database, the two relevant variables are these:

id  statistic_id    source  unit_of_measurement has_mean    has_sum name
8   sensor.accumulated_consumption_xxx  recorder    kWh 0   1   NULL
62  sensor.accumulated_consumption_xxx_cost recorder    NOK 0   1   NULL

For sensor.accumulated_consumption_xxx I get nothing of interest around the time of the restart:

id  created start   mean    min max last_reset  state   sum metadata_id
4734239 2022-04-07 09:10:11.117538  2022-04-07 09:05:00.000000  NULL    NULL    NULL    2022-04-06 22:00:00.000000  15.011436   8184.306336000003   8
4734117 2022-04-07 09:05:10.587470  2022-04-07 09:00:00.000000  NULL    NULL    NULL    2022-04-06 22:00:00.000000  14.960122   8184.255022000003   8
4733995 2022-04-07 09:00:10.514060  2022-04-07 08:55:00.000000  NULL    NULL    NULL    2022-04-06 22:00:00.000000  14.897442   8184.192342000003   8
4733881 2022-04-07 08:55:10.574525  2022-04-07 08:50:00.000000  NULL    NULL    NULL    2022-04-06 22:00:00.000000  14.76474    8184.059640000003   8
4733754 2022-04-07 08:50:11.987512  2022-04-07 08:45:00.000000  NULL    NULL    NULL    2022-04-06 22:00:00.000000  14.756944   8184.051844000003   8
4733632 2022-04-07 08:45:11.064870  2022-04-07 08:40:00.000000  NULL    NULL    NULL    2022-04-06 22:00:00.000000  14.691565   8183.986465000004   8

For sensor.accumulated_consumption_xxx_cost, however, there is a reset about 6 minutes after the restart (08:57:23 below), and the sum is increased by the state (instead of by the difference between the last two states) at that time :

id  created start   mean    min max last_reset  state   sum metadata_id
4734176 2022-04-07 09:10:10.963617  2022-04-07 09:05:00.000000  NULL    NULL    NULL    2022-04-07 08:57:23.496113  34.9024251096001    18393.840959999725  62
4734054 2022-04-07 09:05:10.435734  2022-04-07 09:00:00.000000  NULL    NULL    NULL    2022-04-07 08:57:23.496113  34.7834741262   18393.722009016325  62
4733932 2022-04-07 09:00:10.360282  2022-04-07 08:55:00.000000  NULL    NULL    NULL    2022-04-07 08:57:23.496113  34.6380423942   18393.576577284326  62
4733826 2022-04-07 08:55:10.409791  2022-04-07 08:50:00.000000  NULL    NULL    NULL    2022-04-06 22:00:02.629665  34.1516263789998    18358.938534890127  62
4733701 2022-04-07 08:50:11.783286  2022-04-07 08:45:00.000000  NULL    NULL    NULL    2022-04-06 22:00:02.629665  34.1334998993998    18358.920408410526  62
4733579 2022-04-07 08:45:10.936130  2022-04-07 08:40:00.000000  NULL    NULL    NULL    2022-04-06 22:00:02.629665  33.9814871864998    18358.768395697625  62

I can manually fix the cost hiccup in the database, but it's annoying that I have to.

I'm not sure why this happens, if there is something with my sensors, if its the Tibber integration that is the source of it, or if it's the Energy component. If the sensor.strompris_xxx sensor is not defined for a few seconds during restart, can this reset the cost calculation?

What version of Home Assistant Core has the issue?

core-2022.4.0

What was the last working version of Home Assistant Core?

Has been like this for several months (and I always run the latest version)

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Tibber or Energy

Link to integration documentation on our website

https://www.home-assistant.io/integrations/tibber/

Diagnostics information

Tibber diagnostics:

{
  "home_assistant": {
    "installation_type": "Home Assistant OS",
    "version": "2022.4.0",
    "dev": false,
    "hassio": true,
    "virtualenv": false,
    "python_version": "3.9.9",
    "docker": true,
    "arch": "armv7l",
    "timezone": "Europe/Oslo",
    "os_name": "Linux",
    "os_version": "5.10.92-v7l",
    "supervisor": "2022.03.5",
    "host_os": "Home Assistant OS 7.5",
    "docker_version": "20.10.9",
    "chassis": "embedded",
    "run_as_root": true
  },
  "custom_components": {
    "nobo_hub": {
      "version": "1.1.3",
      "requirements": [
        "pynobo==1.1.2"
      ]
    },
    "remote_homeassistant": {
      "version": "3.6",
      "requirements": []
    },
    "hacs": {
      "version": "1.24.3",
      "requirements": [
        "aiogithubapi>=21.11.0"
      ]
    }
  },
  "integration_manifest": {
    "dependencies": [
      "recorder"
    ],
    "domain": "tibber",
    "name": "Tibber",
    "documentation": "https://www.home-assistant.io/integrations/tibber",
    "requirements": [
      "pyTibber==0.22.2"
    ],
    "codeowners": [
      "@danielhiversen"
    ],
    "quality_scale": "silver",
    "config_flow": true,
    "iot_class": "cloud_polling",
    "loggers": [
      "tibber"
    ],
    "is_built_in": true
  },
  "data": {
    "homes": {
      "e773ffbe-3c77-46bf-944f-6ac29ee341a9": {
        "last_data_timestamp": "2022-04-07T23:00:00+02:00",
        "has_active_subscription": true,
        "has_real_time_consumption": true,
        "last_cons_data_timestamp": "2022-04-07T12:00:00+02:00",
        "country": "NO"
      },
      "779a80a7-7c74-4eb3-9cf1-742d6c28aa3a": {
        "last_data_timestamp": "2022-04-07T23:00:00+02:00",
        "has_active_subscription": true,
        "has_real_time_consumption": true,
        "last_cons_data_timestamp": "2022-04-07T12:00:00+02:00",
        "country": "NO"
      },
      "9ea852ff-3462-4509-b31c-8cd31cc938d5": {
        "last_data_timestamp": "2022-04-07T23:00:00+02:00",
        "has_active_subscription": true,
        "has_real_time_consumption": true,
        "last_cons_data_timestamp": "2022-04-07T12:00:00+02:00",
        "country": "NO"
      }
    }
  }
}

Example YAML snippet

See The problem

Anything in the logs that might be useful for us?

Nothing

Additional information

No response

probot-home-assistant[bot] commented 2 years ago

tibber documentation tibber source (message by IssueLinks)

probot-home-assistant[bot] commented 2 years ago

Hey there @danielhiversen, mind taking a look at this issue as it has been labeled with an integration (tibber) you are listed as a code owner for? Thanks! (message by CodeOwnersMention)