Template to check if switch is On

Is there a template to check if a switch is on for 5 minutes like the for condition that is available in automation state and numerical state triggers?

You have to construct it by comparing the difference of time stamps of now() and last_changed to some value in seconds.

1 Like

I have a thermostat and I would like to check if the temperature is below 25 degrees for 10 minutes with template. Is there any template for this?

You can do that with a state trigger.

trigger:
  platform: numeric_state
  entity_id: climate.your_thermostat
  attribute: current_temperature # or whatever the attribute is
  below: 25
  for:
    minutes: 10

So, I suppose, it cant be done with a template…?

Sure it can. But if it is a condition or trigger, why go to the trouble?

At times I find the numeric state trigger doesnt work even when the templates works. So I as a backup in such cases I want to have a template with the condition to check this for condition. Can you give me any pointers to form this template?

They always work, but they only trigger when crossing the set threshold.
If you have a “below 25” trigger, it will only trigger when crossing from above 25 to below. Going from 20 to 19 for example will not trigger it. If you want this sort of trigger this is one way;

trigger:
  - platform: state
    entity_id: climate.your thermostat # triggers on every state and attribute change
  - platform: time_patterm
    minutes: "/1" # triggers every minute
condition:
  condition: numeric_state
  entity_id: climate.your_thermostat
  attribute: current_temperature # or whatever the attribute is
  below: 25
  for:
    minutes: 10
action:
...

The challenge of using a template is that, in this case, you’re using a climate entity. The last_changed and last_updated values aren’t only affected by changes in temperature but also by changes to any of the entity’s attributes. That means when you try to compute the “last 10 minutes” by referencing either last_changed or last_updated, they may have been altered by something other than a temperature change (like a change in hvac_mode or fan_mode).

1 Like

You could copy the temperature attribute into it’s own sensor using a template sensor to get around this.