Sensor of average state?

Not sure how Id go about templating this, but the overall goal is to be able to have a sensor that shows the state another sensor reported longest over the last 5 minutes. Ex. State A for 2 mins state B for 30 seconds state C for 10 second and state a again for 2 min 20 second should give the output of State A.
Thank you

It can’t be done with templates alone since they do not have access to the past states. If the watched sensor has categorical values you could set up a History Stats sensor for each possible state using either time or ratio type. Then use a template sensor to find the one that has the highest value.

It might also be possible in a single SQL sensor… but that’s beyond my SQL knowledge.

Thanks! I’ll look at this road