Individual condition for each trigger

If I want differents trigger with different conditions now I have two options:

  • Make a automation for each condition
  • Make a automation with a choose based in trigger ID and add condition in each case.

Both solution are verbose.

I think can be interesting add condition in each trigger. Less verbose and more clean.

There is a third option, which is to make a more specific/condition-filled trigger using template triggers…

My understanding might be off here, but that’s not really equivalent, right? Triggers are event-based, conditions are state-based. If you make a template trigger that includes your “condition”, it will also trigger on events of the “condition” that cause the template to render “true”.

But, aren’t events state-based? :thinking:

With a template trigger, the “event” is the template evaluating to true. If you include conditions that maintain the template’s falseness, it will never fire. Having your conditions allow the template to render true when they shouldn’t is just a poorly written template.

No, events are based on states changing. Conditions are based on the current state.

I guess I’m not seeing how to do the following with a single template trigger: I want to fire an automation if the front door opens between 9am and 5pm. If the door opened at 8am, and stayed open, I do not want it to fire when the time changes to 9am. I’d love to be able to simplify some of my automations using just template triggers, but I’m not seeing how do this-- maybe I just write poor templates.

trigger:
  - platform: state
    entity_id: binary_sensor.front_door_contact
    from: 'off'
    to: 'on'
condition:
  - condition: time
    after: '09:00:00'
    before: '17:00:00'

It’s funny, I had today, for the first time wanted this exact option. Wanted, not needed necessarily as I knew that I could probably template the condition in to the trigger, but it seems that every time I want to do something via templates I end up spending hours working it out or end up having to ask this awesome community.

So for me I think it would be nice to achieve this quickly via the Ui.

So one vote from me, but don’t forget you need to vote for your own feature request as well :+1:t2:

1 Like

provide use case examples or it will likely never be implemented.

Not that it will likely be implemented anyway but if you post real world examples then at least you can give a motivation to look into it.

and I’m curious to see how you envision this being different than what we have now.

trigger:
  - platform: template
    value_template: "{{ is_state('binary_sensor.front_door_contact', 'on') and 9 < now().hour < 17 }}"

To be clear, I wouldn’t do it this way however, it’s effectively what this Feature Request is asking for, namely the ability to incorporate several considerations into a single trigger.

{% set last_changed = states.binary_sensor.front_door_contact.last_changed %}
{{ 
  states('binary_sensor.front_door_contact')
    and last_changed > today_at("9:00")
    and last_changed < today_at("17:00")
}}

Just a quick example, but would something like this work? Might need a bit of tweaking, but enough for you to get the idea.

Would that satisfy this condition?

Seems like it would fire at 9am if the door was already open, but I could be missing a detail of how it evaluates.

Perhaps this could work:

{{ states('binary_sensor.front_door_contact') == "on" and (now().hour >= 9 or now().hour <= 17) }}

It would but so would this Feature Request. That’s why there are advantages to having a separate trigger and condition.

This does not satisfy:

If the door opened at 8am, and stayed open, I do not want it to fire when the time changes to 9am

The template that r-j-taylor supplies looks to me like it would work. Thanks!

1 Like

Perhaps if the last state change is templated in to the mix?
If last state change is within the same time span then trigger.

Not that it will be pretty…

Thanks!

Looks like this should work. Didn’t even think of looking at last_changed. Thanks for indulging me.

1 Like

Isn’t it just asking to have the ability to move conditions to the trigger itself? How would trigger-specific conditions break the condition functionality?

Post an example comparing the way you would do it now with the way your proposal would work.

So the template I supplied? :slight_smile:

1 Like

Be advised that it will trigger for any reason the binary_sensor’s state changed, including if it changed to/from unavailable.

I missed that. During the time I posted my answer everything just happened and I had trouble catching up :slight_smile:

1 Like