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.
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…
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.
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.
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:
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.
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.
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?