Integration Sensor Not Updating

Hi all, I’ve spend the past two days trying to figure out why an integration sensor isn’t working for tracking kWh’s consumed in the energy dashboard. Essentially, I have two sensors and templates set up almost identically for tracking kWh consumed, one works fine, the other refuses to update.

Summary of what I’m doing:
I’m trying to create an energy sensor in Home Assistant that tracks my UPS’s total load (in kWh) using the integration platform.

I have a template sensor called sensor.ups_total_load (in Watts) based on UPS load percentage × nominal output power. This is data collected from the APC UPS Daemon, which gathers data from my Windows desktop running Apcupsd and connecting to my UPS over USB. Below is the sensor template from my templates.yaml file.

Sensor in templates.yaml
- sensor:
    - name: "UPS Total Load"
      unique_id: ups1500_total_load
      unit_of_measurement: W
      device_class: power
      state_class: measurement
      availability: >
        {% set load = states('sensor.justinspcv8_load') %}
        {% set nominal_power = states('sensor.justinspcv8_nominal_output_power') %}
        {{ load is number or load not in ['unknown', 'unavailable', None] and nominal_power is number or nominal_power not in ['unknown', 'unavailable', None] }}
      state: >
        {% set load = states('sensor.justinspcv8_load') | float(0) %}
        {% set nominal_power = states('sensor.justinspcv8_nominal_output_power') | float(0) %}
        {% if load > 0 and nominal_power > 0 %}
          {{ (load / 100) * nominal_power }}
        {% else %}
          0
        {% endif %}

I set up an integration sensor to accumulate energy from it, but it stays stuck at “unknown”. I’ve also tried manually updating the sensor.ups_total_load_energy_kwh entity state in dev tools to a value like 0.1 to see if that would trigger it to start working, but no it never updates. Below is my integration sensor from configuration.yaml,

Sensor from configuration.yaml
sensor:
  - platform: integration
    unique_id: ups_total_load_energy_kwh
    source: sensor.ups1500_total_load
    name: "UPS Total Load Energy (kWh)"
    unit_prefix: k
    unit_time: h
    round: 2

In Developer Tools->Statistics, I noticed the unit of measurement is blank for the integration sensor, even though I set unit_prefix: k and unit_time: h. Not sure if this has something to do with it?

I also tried adding an availability: condition to my template sensor to make sure it’s not unknown or unavailable at any time, but it still doesn’t work.

Below is my config from another sensor also using similar inputs that just tracks the power usage from my desktop computer only (Server and Desktop are connected to the same UPS). This sensor and integration is working perfectly!

templates.yaml
- sensor:
    - name: "Justins Desktop Wattage"
      # Subtracts the server wattage sourced from iDRAC from the total UPS power draw
      unique_id: justins_desktop_wattage
      unit_of_measurement: W
      device_class: power
      state_class: measurement
      state: >
        {% set ups_wattage = states('sensor.justinspcv8_load') | float(0) / 100 * (states('sensor.justinspcv8_nominal_output_power') | float(0)) %}
        {% set server_wattage = states('sensor.server_power') | float(0) %}
        {% if ups_wattage not in ['unknown', 'unavailable', None] and server_wattage not in ['unknown', 'unavailable', None] %}
        {{ [ups_wattage - server_wattage, 0] | max }}
        {% else %}
          0
        {% endif %}
configuration.yaml
sensor:
  - platform: integration
    unique_id: justins_desktop_energy_kwh
    source: sensor.justins_desktop_wattage
    name: "Justins Desktop Energy (kWh)"
    unit_prefix: k
    unit_time: h
    round: 2

Hopefully, someone can help me figure out what is going on, thanks!

You can replace all of this:

availability: >
  {% set load = states('sensor.justinspcv8_load') %}
  {% set nominal_power = states('sensor.justinspcv8_nominal_output_power') %}
  {{ load is number or load not in ['unknown', 'unavailable', None] and nominal_power is number or nominal_power not in ['unknown', 'unavailable', None] }}

With:

availability: >
  {{ has_value('sensor.justinspcv8_load') and has_value('sensor.justinspcv8_nominal_output_power') }}

This is not the correct entity id:

sensor:
  - platform: integration
...
    source: sensor.ups1500_total_load
...

Should be:

sensor:
  - platform: integration
...
    source: sensor.ups_total_load
...

The entity id is generated from the template name, not the unique id.

1 Like

Wow, that was it. I feel so stupid! Thank you very much for the help! And is there somewhere I can find more specific information on the entity IDs and unique IDs and what the difference is?
EDIT: I read up a bit more on them around the forums and here, so I understand the point, but I’m confused as to why referring to a sensor’s Unique ID doesn’t work properly; it seems like it should? Or at least this seems like a feature that should be implemented? And if not, it seems like an error should be thrown if a sensor source refers to a Unique ID rather than an Entity ID as it’s supposed to.

The unique id is mostly used as an interface between the backend and frontend. e.g. So you can change the entity name or entity_id from the UI. It is not meant to be used in the backend for entity identification. For that you have the entity id or device id (not recommended).

1 Like