Check "time on" of switch

Is there a way to see how long something has been in a certain state?

I have some automations where i trigger an event to turn of after a certain amount of time. These fail sometimes and never turn off, which can be quite expensive.

I would prefer to have an automation that runs every minute, and check to see how long a switch has been on for, and if it is greater than the limit, turns it off.

This is typically how you would implement this sort of automation in the industrial setting.

cheers

Here is a possible solution. May be a bit hacky but would work. You could create a input_number entity to store the number of minutes the sensor has been on. Every minute when you have your automation run, have it increment the input_number by one if the sensor is on. Create another automation to watch for the input_number state change and when it is over your threshold, turn off the sensor and reset the input_number to 0.