Last state of a sensor, last 5 seconds ago

Hi!

There is any way in order to get the last state of a sensor? But not the time when the state changed. The state from 5 seconds ago

Thanks.

It’s hard to know exactly what you want for sure but…

I think if I understand you right I would create a trigger template sensor that runs every 5 seconds that looks at the sensor you want to know the state of 5 seconds ago.

so every time you look at the template sensor state it will be what the state of the monitored sensor was 5 seconds ago.

There may be another way but this is what I came up with off the top of my head.

Yes… I can do that with variables and automation, but I thought that there is something easier, a line which extract the last state

Not that I’m aware of. Tho it would be a fine Feature Request.

An SQL sensor perhaps?

Query the database for the sensor state 5 seconds ago.

EDIT: nope. It the query option does not support templates, which you would need to get {{ now() - timedelta(seconds=5) }}

Hey, I think in this topic, I can put my question:
I want to calculate the difference between the actual sensor value and the value some time ago. For example energy usage.

with the following code in configuration.yaml I collect all the values every minute:

- trigger:
      - platform: time_pattern
        # Update jede 1 Minuten 
        minutes: "*"
    sensor:
      # 1-minütlicher Stromverbrauch Wert 1
      - name: "Stromverbrauch Minute Wert 1"
        unit_of_measurement: "kWh"
        state: "{{ states('sensor.zahlerstand') | float }}"

So how do I insert the line which tom_I mentioned?:
{{ now() - timedelta(seconds=5) }}

My idea to collect two different values for even and odd minutes was not successful - I am not that good in the yaml code. BTW: Is there a good programmers overview?

1 Like