Get sensor value from X seconds ago

Hey guys!

I have load cells under my bed to detect if someone is laying in the bed or not. Since it’s a big bed with two mattresses, there are two sensor entities, one for each side.

I noticed a problem with using absolute values in comparison:

if bedsensor left changes to lower than 20 kg, enable the light

If my wife lays more on my side than on hers, the sensor triggers. Additionally the sensor value changes over time, it’s not zero anymore when no one is in the bed, it’s about 5 kg now.

Therefore I thought of using relative values.
My problem is how to get a specified older value (like two seconds before), because the sensor might not change its state abruptly but „stepwise“ - so a comparison with from_state.state is not a reliable solution.

Is there a way to retrieve previous values of a sensor?

There may be other ways but the only way I can think of is to create either a triggered sensor or an automation to update an input_number entity that is triggered every few seconds and set to the the value of the desired sensor. the new sensor will be the value of the original sensor no longer than the trigger interval ago.

I hope you get what I’m trying to explain.

This works, of course. I thought there might be a more „official“ implemented way, but this is a pragmatic solution. Thank you!