Different modes for integration sensor

Integration sensor is working great for my power meter, which provides updates every 10 seconds, and is always doing something.

It’s no good for my gas sensor though, which provides updates every 15 minutes, and goes for long periods at 0 kW. Let’s say my gas usage has been 0 from 00:00 to 06:00. At 06:00 it jumps to 10 kW. The integration sensor interprets that as a Trapezium/Triangle gradually increasing from 0kW@00:00 to 10kW@6:00, or 30 kWh. Actually though, I’ve used 0 kWh.


Could we have some modes which integrate differently? Trapezium, or Rectangle (anchored on left, middle or right edge).

Here is the raw sensor.gas_usage graph to go with the above image:

Actually, another way to solve this would be to have a maximum trapezium width / maximum time interval. So when a new reading comes in, you use min(new_state.last_updated - old_state.last_updated,15 minutes) as the time elapsed…


I’ve added left and right methods. Middle is virtually impossible because there is no sample in-between.

1 Like

BTW you need to force updates on your raw sensor, else the problem will persist with different results.

Don’t know which platform is sensor.gas_usage, but that platform needs to implement forced updates for this to work.

It’s a loop energy sensor. I’ll go learn about forced updates. Thanks for making the change.

Try this python script from this post. You should not have to change your power/gas sensor.
Also, consider what happens when the gas meter value is constant over an hour boundary. The Integration Sensor won’t divide the energy into the appropriate hour intervals.

That is why the sensor needs to provide forced updates.

Figuring out how to modify the power sensor will be difficult for most users unless an example automation is provided in the Integration Sensor component documentation. I guess we need to force an update at hour:59:59 ?
Is there a way for the Integration Sensor to break up the integration at the hour boundary?

In my Python Script I use a last_power sensor to record the value of the previous power value. At the hour boundary time I calculate the energy from the last_power value time to the hour boundary time and add it to the energy accumulation for the last hour interval. Then the last_power value time is reset to the hour boundary time and the energy accumulation is also reset.

@GrahamS the integration sensor, contrary to your script doesn’t care what time is it, it just takes the time at the moment the source sensor is updated…

What is necessary is for the source sensor to periodically run an update method even if no values have changed. In internal home-assistant that’s call force_update. Look at MQTT sensor documentation, but other sensor platforms also support this feature.

I can’t see the Integration Sensor source code right now but you did reveal the “last_period” attribute of this sensor which I assumed was the energy accumulation over the last hour?
So I think that the Integration Sensor could handle the lack of updates in a similar manner as the Python Script. It could create another energy change at 1 second before the hour to be compatible with the Utility Meter. I don’t think this would corrupt the generality of the Integration Sensor.

For forced updates I was think of an automation calling the service homeassistant.update_entity at 1 second before the hour. I think this would be better than getting the power sensor to publish a MQTT message every second with the force_update option set to True in the MQTT configuration.

last_period is an attribute of the utility_meter sensors not of the integration sensor (different things!)

Pure sensors such as the integration sensor are not allowed to have services, so can’t add any reset service.

Your suggestion about using homeassistant.update_entity periodically might just work for all cases (haven’t tested it)

OK my mistake.
Worth testing the automation suggestion and adding it to the Integration Sensor documentation.

I tried the updated Integration Sensor in HA 0.88.1 with the method: left.
It is an improvement but still seeing significant errors due to integration over the hour boundary.
I tried this automation workaround:

    initial_state: true
      platform: time_pattern
      minutes: 59
      seconds: 59
      service: homeassistant.update_entity
      entity_id: sensor.power_test

in an attempt for force a change in the power sensor just before the hour boundary. I looked in the database and saw the event for the homeassistant.update_entity service but I did not see an event for the power sensor so the integration sensor was not triggered to finish the energy integration just before the hour time was up.

I don’t know why this workaround failed.

The integration sensor does not have any update code (hence nothin happened on update_entity), it runs asynchronously whenever the source is updated.

The source sensor state needs to change for the integration sensor to react.

What source sensor are you working with? I cannot understand your need to force hourly updates, when the integration sensor will handle the math regardless of when the updates occur…

My source sensor represents the gas flow to my furnace. The gas flow ramps up in stages 40% to 70% then 100%. The ramp stages last 7-10 minutes so it is quite common to have gas flowing over the hour change boundary. If the gas flow is constant at the hour change I want the energy to be divided up on either side so the energy belongs to the correct hour period. This is why I attempted to force a change in the source sensor to then trigger the integration sensor to update the energy calculation just before the hour change.

Which HA sensor platform are you using for sensor.power_test ?

The HA sensor platform is “template”

  - platform: template
        friendly_name: 'Power Test'
        unit_of_measurement: 'kW'
        value_template: '{{ states.input_number.power_test.state | float }}'

input_number.power_test is set in a script:

 # Triggered in automation by switch.furnace -> ON
    alias: "Resetable Delay to Heat0"
      - service: homeassistant.turn_off
            - script.timer_heat0
            - script.timer_heat1
            - switch.heat1
      - service: homeassistant.turn_on
            - switch.heat0
            - script.timer_heat0
      - service: input_number.set_value
          entity_id: input_number.slider_power
          value: 4

    alias: "Heat0 Timer"
      - delay:
          minutes: 7
          # Purge and ignition delay
          seconds: 80  
      - service: switch.turn_off
          entity_id: switch.heat0
      - service: homeassistant.turn_on
            - switch.heat1
            - script.timer_heat1
      - service: input_number.set_value
          entity_id: input_number.slider_power
          value: 7

    alias: "Heat1 Timer"
      - delay:
          minutes: 10
      - service: switch.turn_on
          entity_id: switch.heat0
      - service: input_number.set_value
          entity_id: input_number.slider_power
          value: 10

In my Python script I had to change the sensor value to change the timestamp.
(In this case sensor.last_power is updated.)

    hass.states.set(sensor_last_power, (last_power + 0.1), {"unit_of_measurement": power_unit})
    hass.states.set(sensor_last_power, last_power, {"unit_of_measurement": power_unit})

I understand that all this has nothing to do with the Integration Sensor unless it is promoted for use with the Utility Meter component.

lets try updating that value once an hour by adding a ridiculous value (e.g. 0.000001 or something in the order of a tolerable error). Hopefully that will trigger the template sensor to update, and hence the integration sensor to integrate once an hour.

That would probably work but it is not a general solution for other users.
input_number has a service to change the state. Sensors do not. The only way that I have found to change a sensor timestamp is using a Python script.
It there another way to force a change to a sensor timestamp?