Time pattern trigger template doesn't seem to update

My overall goal is to use a derivative sensor to tell me my current rate of natural gas usage. The challenge I run into is when my furnace or hot water heater shuts off, I don’t get any updates from my gas meter until something else starts to use gas. Since the derivative helper only updates its value when the underlying data changes, my rate of use calculation stays high for longer then the use is actually happening.

Based on this similar answer, I need to set up a time triggered template sensor and calculate the derivative of a sensor that regularly “updates” even though the value doesn’t really change.

I have tried setting up such a time triggered template sensor, but the sensor doesn’t show that it updates at the scheduled interval. With this template, I can see the template sensor it creates, but it only updates when the underlying value changes, not every 5 seconds. (Once I get this working I will update less frequently, but 5 seconds is short enough that I can see (or don’t see in this case) the results.)

Am I taking the correct approach, and if so what am I doing wrong?

Here is what I am trying:

- trigger:
    - platform: time_pattern
      seconds: /5
  sensor:
    - name: "NW Natural Gas Consumption Trigger"
      unique_id: "{{device_id('sensor.nw_natural_gas_consumption')}}_trigger" 
      state: "{{ states('sensor.nw_natural_gas_consumption') }}"
      unit_of_measurement: "CCF"
      device_class: "gas"


Time shown in the below screenshot is more than 5 seconds ago. I would expect that the Last Changed timestamp would be the last time the value changed, and the Last Updated timestamp would be within 5 seconds:

Last Changed only updates if the state value changes.

Last Updated only updates if the state and/or an attribute value changes.

https://www.home-assistant.io/docs/configuration/state_object
Screenshot 2023-10-18 at 13-36-52 State Objects

You could force Last Updated to change by adding an attribute template.

- trigger:
    - platform: time_pattern
      seconds: /5
  sensor:
    - name: "NW Natural Gas Consumption Trigger"
      unique_id: "{{device_id('sensor.nw_natural_gas_consumption')}}_trigger" 
      state: "{{ states('sensor.nw_natural_gas_consumption') }}"
      unit_of_measurement: "CCF"
      device_class: "gas"
      attributes:
        time: "{{ now() }}"

However I’m not sure if this will update your derivative sensor, as the state will still be the same. Try it and see.

If it does not, then you could add a small random value to the state and hope that the pseudo-random number has no bias:

- trigger:
    - platform: time_pattern
      seconds: /5
  sensor:
    - name: "NW Natural Gas Consumption Trigger"
      unique_id: "{{device_id('sensor.nw_natural_gas_consumption')}}_trigger" 
      state: "{{ states('sensor.nw_natural_gas_consumption')|float(0) + range(-10, 10)| random / 100 }}"
      unit_of_measurement: "CCF"
      device_class: "gas"

Looks like adding the changing attribute containing the current time does re-trigger the derivative sensor. Now the trick is to find a set of derivative running average settings that yields useful information.

Thanks