core icon indicating copy to clipboard operation
core copied to clipboard

Source sensor sensor.mains_energy_export has no unit of measurement.

Open crayner opened this issue 10 months ago • 11 comments

The problem

Hi, This report comes from the logs on my Home Assistant. I created the integration in yaml, not using the user interface at all. Attached is the integration:

- name: Mains Energy Export
  unique_id: mains_energy_export
  platform: integration
  source: sensor.mains_power_export
  unit_prefix: k
  round: 2
  max_sub_interval:
    seconds: 5
  unit_time: h
  method: trapezoidal

What version of Home Assistant Core has the issue?

core-2025.2.1

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

integral

Link to integration documentation on our website

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

Diagnostics information

No response

Example YAML snippet

- name: Mains Energy Import
  unique_id: mains_energy_import
  platform: integration
  source: sensor.mains_power_import
  unit_prefix: k
  round: 2
  max_sub_interval:
    seconds: 5
  unit_time: h
  method: trapezoidal
- name: Mains Energy Export
  unique_id: mains_energy_export
  platform: integration
  source: sensor.mains_power_export
  unit_prefix: k
  round: 2
  max_sub_interval:
    seconds: 5
  unit_time: h
  method: trapezoidal

Anything in the logs that might be useful for us?

Logger: homeassistant.components.utility_meter.sensor
Source: components/utility_meter/sensor.py:508
integration: Utility Meter (documentation, issues)
First occurred: 10:48:38 (2 occurrences)
Last logged: 10:50:44

Source sensor sensor.mains_energy_export has no unit of measurement. Please create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+integration%22
Source sensor sensor.mains_energy_import has no unit of measurement. Please create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+integration%22

Additional information

No response

crayner avatar Feb 09 '25 00:02 crayner

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

Code owner commands

Code owners of integration can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Renames the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign integration Removes the current integration label and assignees on the issue, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


integration documentation integration source (message by IssueLinks)

home-assistant[bot] avatar Feb 09 '25 00:02 home-assistant[bot]

Hi, These are the power sensors, also built only in yaml.

- name: Mains Power Total
  unique_id: mains_power_total
  unit_of_measurement: W
  state_class: measurement
  device_class: power
  state: |
    {% set answer = states('sensor.mains_power_channel_a_power') %}
    {% if is_number(answer) %}{% set a_power = answer | float %}{% else %}{% set a_power = 0 | float %}{% endif %}
    {% set answer = states('sensor.mains_power_channel_b_power') %}
    {% if is_number(answer) %}{% set b_power = answer | float %}{% else %}{% set b_power = 0 | float %}{% endif %}
    {% set answer = states('sensor.mains_power_channel_c_power') %}
    {% if is_number(answer) %}{% set c_power = answer | float %}{% else %}{% set c_power = 0 | float %}{% endif %}
    {{ (a_power + b_power + c_power) | float }}
  attributes:
    attribute: "{{ now() }}"
- name: Mains Power Import
  unique_id: mains_power_import
  unit_of_measurement: W
  state_class: measurement
  device_class: power
  state: |
    {% set answer = states('sensor.mains_power_total') %}
    {% if is_number(answer) and answer | float > 0%}{% set a_power = answer | float %}{% else %}{% set a_power = 0 | float %}{% endif %}
    {{ (a_power) | float }}
  attributes:
    attribute: "{{ now() }}"
- name: Mains Power Export
  unique_id: mains_power_export
  unit_of_measurement: W
  state_class: measurement
  device_class: power
  state: |
    {% set answer = states('sensor.mains_power_total') %}
    {% if is_number(answer) and answer | float < 0 %}{% set a_power = answer | float %}{% else %}{% set a_power = 0 | float %}{% endif %}
    {{ (a_power) | float | abs }}
  attributes:
    attribute: "{{ now() }}"

and the states in Developer tools shows the correct unit of measurement:

sensor.mains_energy_importMains Energy Import | 0.88 | state_class: total unit_of_measurement: kWh device_class: energy friendly_name: Mains Energy Import source: sensor.mains_power_import
-- | -- | --
[sensor.mains_energy_import](http://homeassistant.local:8123/developer-tools/state#)
Mains Energy Import
0.88	state_class: total
unit_of_measurement: kWh
device_class: energy
friendly_name: Mains Energy Import
source: sensor.mains_power_import

crayner avatar Feb 09 '25 02:02 crayner

Utility Meter detected that the source sensor had no unit of measurement, so the issue lies in the template not in utility_meter nor integration sensor which simply relay the source unit of measurement.

Eventually this is a race condition where template takes several cycles to propagate the unit

dgomes avatar Feb 09 '25 13:02 dgomes

Hey there @phracturedblue, @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (template) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of template can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Renames the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign template Removes the current integration label and assignees on the issue, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


template documentation template source (message by IssueLinks)

home-assistant[bot] avatar Feb 09 '25 13:02 home-assistant[bot]

@dgomes the template integration simply assigns the UOM, this isn't a template issue. This is likely elsewhere.

@crayner Please post a screenshot of the source entities in developer tools -> states page showing attributes.

Petro31 avatar Feb 10 '25 11:02 Petro31

both Riemann and Utility_meter rely on the source UOM, since everything comeback together (from the screenshot the UOM is set) I'm betting on a race condition.

dgomes avatar Feb 10 '25 14:02 dgomes

@dgomes the template integration simply assigns the UOM, this isn't a template issue. This is likely elsewhere.

@crayner Please post a screenshot of the source entities in developer tools -> states page showing attributes.

Already provided above....

crayner avatar Feb 10 '25 21:02 crayner

I believe this may have been a warning during the process of building the integral. To be clear, this is NOT a Utility Meter, but an integral. As stated above, the integral in the development does show the correct unit-of _measurement, but this information was collected hours after the lag warning was noted.

crayner avatar Feb 10 '25 21:02 crayner

From your logs it shows that it was the utility_meter that detected the issue

dgomes avatar Feb 10 '25 21:02 dgomes

From your logs it shows that it was the utility_meter that detected the issue

yes, but it was the integral at fault. Unique Circumstance: The integral and the utility_meter were created at the same time, as they were loaded from yaml on the same restart of HA. Hence, my comment earlier, about the delay in the build process.

crayner avatar Feb 10 '25 21:02 crayner

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.