Automation triggger: Value template + for - is this possible?

Hi there,
Is it possible to combine a value template trigger and a ‘for’? I mean for a ‘classical’ trigger ‘Numeric state’ we can add a ‘for’ condition meaninng the trigger will only run when its firing condition is met for a certain time.
Can we have the same in a value template trigger?

This would be super useful, but I don’t know how to do this…
Basically I’m just trying to have a trigger when one of temp. sensors indicates a lower temperature than another one, but only for a certain time. This would help reducing unwanted triggers for momentary temperature fluctuations.

The way I’ve seen this done is to create a template sensor, and then use a state or numeric_state trigger with the template sensor.

1 Like

thanks @pnbruckner! Good hint. I like it, but I’m also curious of other possible ideas. Probably declaring ‘helper’ entities is the only option. Anyway if anyone has some other swifty solution - please feel free to put it below.

Actually I’m not sure why the template trigger doesn’t have a for option. It comes up enough that it probably should. I haven’t looked at the details recently, but from what I recall, it should be just as easy for it to have a for option as the state & numeric_state triggers. I’ll take another look…

So I just implemented the for: option for template triggers. I need to add tests and update the documentation and then I can submit it in a PR. If anyone is interested, check out https://github.com/pnbruckner/home-assistant/tree/template-trigger-for, especially the last commit.

1 Like

Wow, that was fast :slight_smile: Thanks a lot for this. I think it really adds value to template triggers.
In which HA release do you think we could see this? Still in 0.94.x or rather 0.95?

lol! Writing the code and getting it working is the easy part. Figuring out how to write the tests, writing the tests and getting them working, and updating the documentation is more involved. Getting the PR accepted and in a release, that’s another story all together, especially when the focus of the main development team is on getting a 1.0 release out. :slight_smile:

But this is worth doing, so someday…

what files did you change? found it

Just force pushed a new commit that replaces the last one. Fixed a pylint error and added a few tests.

I think you should just create a PR at this point. Not sure if the 2 devs will let it through but it’s worth a shot. Looks good to me, then again they don’t approve my stuff often :wink:

Yeah, that’s what I was planning on doing, but first I have to update the doc page.

1 Like

exactly. my PR for adding force_update and expire_after to RFLink sensors is ready, but I still can’t quite figure out how to write proper tests for it… :\

Well I’ve had experience using pytest before, so that helps. But I’m not as familiar with the rest of the HA context, especially the HA-specific test infrastructure. But in this case it turned out to not be too bad. I just based my new tests on the existing tests for the template trigger, as well as the existing tests for the state trigger that involved the use of the for option. I only added three tests, so we’ll see if they consider that enough. :slight_smile:

PR #24330 submitted.

2 Likes

ok, seems like a lot needs to happen to have this merged in the public release. Looks like HA software development process is really serious - which is really good as it makes it more stable and reliable.
Anyway - congrats and good job :slight_smile: I’m really looking forward to see this in my HA installation.

This is so needed, thanks for putting in the effort.

Would be really extra useful if we could template the for: also:

automation:
  - alias: Template trigger with for option
    trigger:
      platform: template
      value_template: "{{ is_state('sensor.abc', '123') }}"
      for:
        minutes: >
          {{states('input_number.for_duration')}}

does your PR enable that too?
If not: quickly add it while it is in the process…:wink: ?

Agreed that would be useful, but that’s a LOT more work than you might imagine. And if it’s done for this trigger, it should also be done for all the others. Adding the for option for this trigger was somewhat simple and contained, so I didn’t mind doing it. But I’m not up for templatizing the for option for all the triggers (and conditions???) at this point.

you’re right,I didn’t imagine correctly probably. It’s just that since this is already available for several options (whats the correct HA word here, component, domain, … ?) like eg the wait_template, and conditions (!), I guess I supposed that could be directly transitioned, or maybe simply used directly…

Supposed wrongly…sorry.
Maybe, if you can ever find the time, and still remember :wink:

1 Like

FYI, the change to add the for option to the template trigger is now in 0.95.0b0.

2 Likes