Set sensor status based on time in a given state over the last x minutes


I have a sensor that can have statuses X, Y, and Z.

I want to write an automation that is triggered by (or really any conditional) when, over the last 60 Minutes, the status of the sensor has been X for at least 45 minutes. The statuses change frequently between the three and in unpredictable ways.

Is there a way to create this type of template with a look back over the last hour aggregating the time in each state?

History stats sensor can do this. Yaml only.

To expand on Petro’s response: