Trigger Automation if Template_Value persists for X seconds?


I want to built an automation, which simplified looks like this:

- alias: Turn HTPC on when AVR is in HTPC-Mode for 2 seconds
    platform: template
    value_template: "{{ is_state_attr('media_player.avreceiver', 'source', 'HTPC') }}"
      seconds: 2
  - service: switch.turn_on
    entity_id: switch.htpc

Unfortunately the “for-condition” don’t seem to apply on a template trigger, cause I get an error message:

ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: [for] is an invalid option for [automation]. Check: automation->trigger->0->for.

I don’t need the 2 seconds to make this work, it’s a little protection if I mistakenly push HTPC on the remote control.

Anyway, is there a workaround?

I don’t think for: is an option for template triggers

You can alternatively add a delay in your action then a condition check before the rest of your action to achieve the same thing, I would think.

I’m fairly new at this myself, but I think you want to do this in three parts:

(1) a script that waits two seconds then turns on the HTPC;
(2) an automation trigger that runs the script (immediately) when the AVR is in HTPC mode; and
(3) another automation trigger that cancels (“turns off”) the script when the AVR is NOT in HTPC mode.

If it’s not in HTPC mode for more than two seconds then the script never finishes and the HTPC doesn’t turn on.

@stunts1337 is correct - I don’t believe that you can use for with a template trigger. What you can do with only one extra step is use that same template for a template_sensor, then adjust your trigger to be a state trigger that watches the template sensor’s state.


Thanks for your hints.

Maybe I should post a Feature Request.