Patform Template - how can I get previous state (its called from_state in actions)

Hi guys,

switching from OH to HA. over the weekend. Steep learning curve behind me and ahead. I am really impressed by HA. One thing I am banging my head around for 6h now and i Need your help.

I have a sensor which gets every minute an update. Its a counter which counts 1/250 kWh and its just a number. I get it from KNX, but that should not matter.

With a sensor template I calculate this into kWh ( a number i can read and see on my meter).

- platform: template
  sensors:
    counter_power_kwh:
      friendly_name: "Strom"
      unit_of_measurement: 'kWh'
      value_template: >
        {{ (states("sensor.counter_power_impulse")|float - 16369685.0) / 250.0 + 35344.6 }}

I have another item which gets updated each time the above one gets updated. Target is to calculate delta kWh and delta T in seconds.

I checked in debugging. When I replace the from_state with a copy paste value from the previous run I get reasonable numbers (480W), however in HA I get 0.0 because from_state is always 0.0.

- platform: template
  sensors:
    counter_power_consumption:
      unit_of_measurement: 'W'
      value_template: >
        {% set delta_kWh = (states("sensor.counter_power_kwh")|float) - (states('states.sensor.counter_power_kwh.from_state')|float) %}
        {% set delta_s = (as_timestamp(now()) - as_timestamp(states.sensor.counter_power_kwh.last_changed)) %}
        {% if states('states.sensor.counter_power_kwh.from_state')|float == 0.0 %}
          0.0
        {% else %}
          {{ (delta_kWh / delta_s) * 1000.0 * 3600.0 }}
        {% endif %}

Can someone point me into the right direction? I have

  • an entity whith increasing kWh
  • I want a regular check how much kWh were consumed.
  • Say every 6min would be ok.It really does not matter how often.
  • with that delta kWh and delta T I can calculate the power consumption.
  • I need only delta from sensor (current and previous value).

How can I get this done? Is an automation the right way? How do I set a sensor item from an action?

I am stuck and need some sleep.

Thank you for reading so far.

Regards
Ralf

The derivative of energy is power. Unfortunately a derivative sensor has yet to be implemented. Add your vote here:

Until then the statistics sensor has change and change rate attributes you could use:

You could also perhaps use https://www.home-assistant.io/integrations/utility_meter/

to calculate changes and cumulative consumption.

You can configure watts into kWh in that sensor.

No you cant and he wants to go the other way anyway (kWh to W).

Hello guys,

I am nearly there. I feel its 10minutes to HEUREKA. I am struggling with the last bit only which his using the service input_number.set_value to store the results which I now am able to calculate.

Short summary

  • I have a kWh meter which regularly announces its value
  • with the deltas between such two announcements one can calculate the power consumption
  • I want to store this value in an input_number. This stepp is missing.

This is the sensor which I have. It is set from actually a KNX sensor, but that should not matter.

[configuration.yaml]

sensors:
- platform: template
  sensors:
    counter_power_kwh:
      friendly_name: "Strom"
      unit_of_measurement: 'kWh'
      value_template: >
        {{ (states("sensor.counter_power_impulse")|float - 16369685.0) / 250.0 + 35344.6 }}

With the following action I calculate the delta in seconds and the delta in kWh between two announcements. With those I can calculate the power consumption.

[configuration.yaml]

automation:
- alias: counter_power_consumption
  trigger:
    - platform: state
      entity_id: sensor.counter_power_kwh
  action:
    - service: system_log.write
      data_template:
        level: error
        logger: homeassistant.components.mylogger
        # Upper bound 69 kW for a typic house is exceed when delta_kWh > 69.000 / 1.000 / 3.600 * (time between two updates)
        message: >-
          {% set delta_kWh = (trigger.to_state.state|float) - (trigger.from_state.state|float) %}
          {% set delta_s = (as_timestamp(now())|float - as_timestamp(states.automation.counter_power_consumption.attributes.last_triggered)|float) %}
          {% if trigger.from_state.state|float <= 0.0 or delta_s == 0.0 or delta_kWh > 69 / 3600 * 60 %}  
            "============== sensor mom verbrauch: NaN }}"
          {% else %}
            "============== sensor mom verbrauch: {{ (delta_kWh / delta_s) * 1000.0 * 3600.0  }}"
          {% endif %}

The trick to write int the logs helped tremendously to get this troubleshooted. Kudos.

The logs show useful numbers

============== sensor delta_s: 59.8812530040741
============== sensor delta_kwh: 0.007999999994353857
============== sensor mom verbrauch: 480.7244342533442

I miss the last step storing this into an input_number which is defined like this.

[configuration.yaml]

input_number:
  counter_power_consumption:
      name: Momentanverbrauch
      unit_of_measurement: "W"
      icon: mdi:gauge
      initial: 0.0
      min: 0.0
      max: 2000
      step: 20

I tried to add the following step into the action.

[configuration.yaml]

automation:
- alias: counter_power_consumption
  trigger:
    - platform: state
      entity_id: sensor.counter_power_kwh
  action:
    ... 
    - service: input_number.set_value
      data_template:
        entity_id: input_number.counter_power_consumption
        value: >-
          {% set delta_kWh = (trigger.to_state.state|float) - (trigger.from_state.state|float) %}
          {% set delta_s = (as_timestamp(now())|float - as_timestamp(states.automation.counter_power_consumption.attributes.last_triggered)|float) %}
          {% if trigger.from_state.state|float <= 0.0 or delta_s == 0.0 or delta_kWh > 69 / 3600 * 60 %}  
            0.0
          {% else %}
            {{ (delta_kWh / delta_s) * 1000.0 * 3600.0 }}
          {% endif %}

Above does not give an error message. However input_number.counter_power_consumptionstays at 0.

I added an additional step behind to prove that. In the gui I see 0 as well.

    - service: system_log.write
      data_template:
        level: error
        logger: homeassistant.components.mylogger
        message: "============== sensor mom verbrauch: {{ states.input_number.counter_power_consumption|float }}"

This one reports constantly.

============== sensor mom verbrauch: 0.0

What am I missing? How do I store a value in an input variable from an action?

I hope I can solve that soon with your help. I tried alone with little success.

Regards
Ralf

Looking in the log I get 0.0 output. But it seems actually working. I will report back tomorrow.