How to access old_state of a sensor in a template for implementing "exponential moving average" statistics filter?

Hi all,
I can’t seem to find a way to access an old_state of the sensor in a template.

When trigger on state change happens, there is always “new_state” and “old_state” that sensor was in before…
For sun for example:

{{ states.sun.sun.state }} - this gives me sun’s current state. above_horizon
{{ states.sun.sun.old_state }} <---- This is what I want… But it does not seem to work… I tried this in “templates” screen of HA and it outputs NO results. And documentation for “state” object indicate that it’s not there : https://home-assistant.io/docs/configuration/state_object/

I want to implement an “exponential moving average” statistics filter, which only requires previous and current value of a sensor to produce a very nice smoothing of sensor data. I know about the statistics sensor… It does not compute what i need. I also aware of the upcoming “filter” sensor. I think I will eventually do a PR on that upcoming filter later to create a filter I want, but for now, I want to do it in a template, while it is NOT available. Or I may just stick with my template since the formula is quite simple.

So is there a way to get access to an old_state of any sensor in a simple way? or Complicated way?

I was trying retrieving previous states using new SQL sensor:


But I noticed that sqlalchemy does not seem to flush data to DB often enough and I was getting empty results back, when I was trying to select last state using SQL sensor.

Also, since all these things are async, how would I even guarantee that an SQL sensor can get me access to a previous value of the sensor right at the moment of state change of the original sensor I am trying to capture?

Thank you all in advance.
-D.

1 Like

Try {{ trigger.from_state.state }}

2 Likes

Brilliant, thank you! It was right in my face, but not obvious somehow. But I can only capture this during the trigger event in automation itself. I will need to pass this value to a script ,which I should be able to do I think.

But I would prefer if there was a way to pass a trigger by name… ie: I have a trigger associated with each automation. inside each automation, I can refer to a trigger just like this: {{ trigger.from_state.state }} but if I wanted to refer to this trigger from a template sensor say… is there a way to refer to it something like: {{ weather.trigger.from_state.state }} , where weather is the name of my automation. This way I could create a template sensor that computed a state as needed without need to capture and store this old_state value in some other sensor temporary.

Basically I would want to create a template sensor as below and be able to refer to a trigger associated with a specific automation… Lets say called “weather”:
(below is just a sample computation, not actual computation I am looking to do)

   my_analytics_sensor:
        friendly_name: 'My previous state sensor'
        value_template: >-
         {% if (weather.trigger.from_state.state | float + weather.trigger.to_state.state | float)/2.0 > 18.0 -%}
            Hot
          {%- else -%}
            Cold
          {%- endif -%}

Why not extend the statistics sensor?

Yes, that would be a long term approach, but actually a better statistics sensor called “filter” is coming very soon. Already merged PR. I probably will do a PR for that one some time in the future… But for now I am interested to figure out if I can do this within constraints of the current HA template capabilities as the other route is a long wait and I actually need this functionality now as my wind alerting chatbot is not doing a good job based on real data coming off the wind meter.

There is a custom component called ‘variables’ that should do what you want:

1 Like

Super! Will take a look. Sounds like exactly what i need.