Template sensor - store previous attribute value in another attribute

Hi all,

I’m tearing my hair out trying to get this to work and I’m sure it’s really simple!

I have a gas kwh accumulation sensor

I’m trying to use this to get the current flow of gas in kwh. In order to do this I assume I need to capture the previous entity value and the current one and calculate the difference between them and the time lapsed.

The issue is that I cannot fathom out how to store the previous current_import value in order to do this. The best I’ve been able to do is either get previous_import to equal current_import i.e. always the same figure or previous_import to equal previous_import i.e. previous_import is a static number that never changes

heres the code i’ve come up with so far

- platform: template
  sensors:
    gas_flow_rate_attributes:
      friendly_name: "Gas Flow Rate Attributes"
      unit_of_measurement: "kWh"
      value_template: "{{ states('sensor.smart_meter_gas_import_today') | float }}"
      attribute_templates:
        previous_import: >
          {{ state_attr('sensor.gas_flow_rate_attributes', 'current_import') | float(states('sensor.smart_meter_gas_import_today')) }}
        current_import: "{{ states('sensor.smart_meter_gas_import_today') | float }}"
        last_import_update: >
          {{states.sensor.gas_flow_rate_attributes.last_changed}}
        elapsed_time_seconds: "{{ as_timestamp(now()) - as_timestamp(states.sensor.gas_flow_rate_attributes.last_changed) }}"
        current_flow: >
          {% set previous_import = state_attr('sensor.gas_flow_rate_attributes', 'previous_import') | float(states('sensor.smart_meter_gas_import_today')) %}
          {% set current_import = states('sensor.smart_meter_gas_import_today') | float %}
          {% set last_changed_attr = state_attr('sensor.gas_flow_rate_attributes', 'last_changed') %}
          {% if last_changed_attr %}
            {% set last_update = as_timestamp(last_changed_attr) %}
            {% set time_delta = (as_timestamp(now()) - last_update) / 3600 %}
            {% if time_delta > 0 %}
              {% set consumption_rate = (current_import - previous_import) / time_delta %}
              {{ consumption_rate }}
            {% else %}
              none
            {% endif %}
          {% else %}
            none
          {% endif %}

Please help!!

See my solution to the similar question in this post:

Thanks.

So sounds like a trigger template is the way forward.

Rather than a timer trigger, can it be triggered by a state update of the entity?

Could you give me a steer on that at all in terms of how the code may look?

ahh yes:

  - trigger:
      platform: state
      entity_id: sensor.smart_meter_gas_import_today
    sensor:
      - name: gas_import_trigger
        state: "{{ states('sensor.smart_meter_gas_import_today') }}"
        attributes:
          previous_import: "{{ state_attr('sensor.gas_import_trigger', 'current_import') | default('N/A') }}"
          current_import: "{{ states('sensor.smart_meter_gas_import_today') }}"
          previous_import_timestamp: "{{ state_attr('sensor.gas_import_trigger', 'current_import_timestamp') }}"
          current_import_timestamp: "{{ states.sensor.smart_meter_gas_import_today.last_changed }}" 

And here with the flow calculation.

Does this look right/is there a better way to do this?

  - trigger:
      platform: state
      entity_id: sensor.smart_meter_gas_import_today
    sensor:
      - name: gas_import_trigger
        state: "{{ states('sensor.smart_meter_gas_import_today') }}"
        attributes:
          previous_import: "{{ state_attr('sensor.gas_import_trigger', 'current_import') | default('N/A') }}"
          current_import: "{{ states('sensor.smart_meter_gas_import_today') }}"
          previous_import_timestamp: "{{ state_attr('sensor.gas_import_trigger', 'current_import_timestamp') }}"
          current_import_timestamp: "{{ states.sensor.smart_meter_gas_import_today.last_changed }}"
          current_flow: >-
            {% set previous_import = state_attr('sensor.gas_import_trigger', 'previous_import') | float %}
            {% set current_import = state_attr('sensor.gas_import_trigger', 'current_import') | float %}
            {% set previous_import_timestamp = state_attr('sensor.gas_import_trigger', 'previous_import_timestamp') | as_timestamp %}
            {% set current_import_timestamp = state_attr('sensor.gas_import_trigger', 'current_import_timestamp') | as_timestamp %}
            {% set time_difference = (current_import_timestamp - previous_import_timestamp) / 3600 %}
            {% if (now().timestamp() - current_import_timestamp) > 120 %}
              0
            {% else %}
              {{ ((current_import - previous_import) / time_difference) | round(2) }}
            {% endif %}
1 Like