Update Template Sensor Once Entity Passes Threshold For 60 Seconds

I have created a template sensor which changes based on the power consumption recorded by my Shelly device. For example, if the current consumption goes from 250 watts to 400 watts it changes from “Power usage is low” to “Power usage is medium”.

I want to improve this so that the current consumption has to pass the threshold for 60 seconds before updating the sensor value. Eg the current consumption will need to be above 400 watts for 60 seconds before the sensor updates from “Power usage is low” to “Power usage is medium”.

This is my current template sensor config:

- sensor:
      - name: "Power Usage"
        state: >
          {% if states("sensor.shellyem_98cdac38f557_channel_1_power") | float < 250 %}
                        Power Usage Is Low.
                      {% elif states("sensor.shellyem_98cdac38f557_channel_1_power")|float > 250 and states("sensor.shellyem_98cdac38f557_channel_1_power")|float < 500 %}
                        Power Usage Is Medium
                      {%- else -%}
                        Power Usage Is High
                      {%- endif %}
                      

Any advice would be greatly appreciated!

See the info on trigger-based template sensors here:

Have a go with that, and if you get stuck, post the code you’ve tried.