Integration (Riemann sum) - value only updating when source sensor updates

I have a simple light named Hallway Light:
image

I know that this light uses 13 W of power when it’s on, and 0 W when it is off, so I created a template sensor to reflect its power usage:

template:
  sensor:
    - name: Hallway Light Power
      unit_of_measurement: W
      device_class: power
      state: "{% if is_state('light.hallway_light', 'on') %} {{ 13.0 }} {% else %} {{ 0.0 }} {% endif %}"

image

I also wanted to track its total energy usage in kWh, so I created an “integration” sensor:

sensor:
  - platform: integration
    source: sensor.hallway_light_power
    name: Hallway Light Energy
    unit_prefix: k
    method: left

image

This works well, except for one issue: the value of the integration sensor only updates when the value of the source (power) sensor changes. If I turn on my light for 12 hours, the energy usage of the light is not gradually added to the total kWh value during those 12 hours like I would expect. Instead, it is all added to the total immediately when I turn the light off. I have daily and monthly utility_meter sensors and this issue causes the energy usage to be added to the wrong day or month. Is there a way to make the integration sensor update in real time (or even on an interval, say, every 5 minutes), so that it will always accurately reflect the amount of energy that my light has used?

1 Like

You’d need to add a time trigger to your template sensor:

- trigger:
    - platform: time_pattern
      hours: '*'
      minutes: '*'
      seconds: 1
  sensor:
    - name: Hallway Light Power
      unit_of_measurement: W
      device_class: power
      state: "{{ 13.0 if is_state('light.hallway_light','on') else 0.0 }}"

That will update the sensor every minute (and I’ve also slightly shortened your state template)

5 Likes

Thank you so much. This sounds like exactly what I need.

I had never heard of this trigger feature before. I can’t find it in the documentation either. Is it documented somewhere?

Edit: Never mind, I found it. Template - Home Assistant

1 Like

There is also this custom integration that does all the hard work for you:

1 Like

@tom_l Wow. I spent several days creating power sensors, energy sensors, and utility meters for all of my lights, and now I found out there is a custom component that can do all of that for me. :smiley:

Thanks for letting me know about this! This is going to make my configuration so much simpler. I can see there is even a scan_interval setting which addresses my original question about updating the state when there is no change.

Are you sure this should be working?
I had the same idea, but since the state doesn’t change (even if triggered in regular intervals) the “integration sensor” never seems to update either.

There might be something wrong with my trigger, as the attribute “last _update” does not change:

  - trigger:
      - platform: time_pattern
        seconds: "/15"

Any idea how I can track the execution of the time-pattern trigger in dev-tools/events?

Adding a dummy attribute-template that changes on every update (time based) to the sensor will at least cause an update on every interval:

  - trigger:
      - platform: time_pattern
        minutes: "/1"
    sensor:
      - name: home_calc_base_power
        unique_id: home_calc_base_power
        unit_of_measurement: "W"
        device_class: power
        state_class: measurement
        icon: mdi:flash
        state: |
          {% set consumer = {
              'fridge':0.4,
              'boiler':0.6,
              'media':0.0,
              'network':0.0,
              'ventilator':0.0,
              'sculpture':1.3,
              'shelly1':4*0.0,
              'shelly25':2*0.0,
              'shellyRGBW':1*0.0,
              'shellyDimmer':2*0.0,
              'sonoff':2*0.0
             } 
          %}
          {{ (consumer.values()|map('float',0)|sum)|round(2) }}
        attributes: 
          triggered_at: "{{ now() }}"

Not sure if this will be enough for the integration-sensor to reevaluate thou, as the state did not change.

2 Likes

What about simply doing your calculation and then adding on to the end of it with something like:

{% set r = 0.000001 + (range(0.000001,0.000009)|random) %}

It’s small enough that it shouldn’t really affect accuracy, but will still trigger a state change?

Doesn’t seem to be necessary. I just tested it, and the attribute change seems to be enough to trigger a reevaluation of the integration-sensor.

Thanks for your input!

2 Likes