Trigger with different `for` depending on condition

I wonder if there’s a possibility to change the for part of the state condition depending on something. This is a “pseudo-config” for automation, that will hopefully show what I mean:

triggers:
  - trigger: state
    entity_id: binary_sensor.some_sensor
    to: "off"
    - if:
        - codition: state
          entity_id: binary_sensor.some_different_sensor
          state: 'off'
      then:
        - for: '00:00:10'
      else:
        - for: '00:00:30'

This would be so much easier then say checking the state at the actions part and react to that. Because I don’t know if above approach would work, I’m currently experimenting with something like this:

triggers:
  - trigger: state
    entity_id: binary_sensor.some_sensor
    to: "off"
    for: '00:00:10'
actions:
  - if:
      - codition: state
        entity_id: binary_sensor.some_different_sensor
        state: 'off'
    then:
      - condition: state
        entity_id: binary_sensor.some_sensor
        state: 'off'
        for: '00:00:20'
[...]

which should add additional 20s on top of the first 10s from trigger.
What do you think? Or maybe there’s a better approach for situation like that?

The problem with doing something like this is that the condition can change between both for values, so neither will trigger. Also, this would be very hard to implement. Your own suggested workaround will not work. the condition will never be true (unless you add a delay).

If you want it anyway, despite possibly not firing at all, what you could do is add both triggers. Give them trigger ids corresponding to the wanted state of the binary sensor. Then add a condition that checks if the binary sensor is equal to the trigger id in order to continue. It will be an equal amount of code in yaml and is currently possible.

triggers:
  - trigger: state
    entity_id: binary_sensor.some_sensor
    to: "off"
    for: "00:00:10"
    id: "off"
  - trigger: state
    entity_id: binary_sensor.some_sensor
    to: "off"
    for: "00:00:30"
    id: "on"
conditions:
  - "{{ trigger.id == states('binary_sensor.some_different_sensor') }}"

The for variable also accepts templating:

triggers:
  - trigger: state
    entity_id: binary_sensor.some_sensor
    to: "off"
    for: 
      seconds: "{{ 10 if is_state('binary_sensor.some_different_sensor', 'off') else 30 }}"

Also, entity_id is misspelled in the original post and Edwin’s response… make sure to fix it.

2 Likes

Yeah, really? In a trigger? And are you sure it changes when the other sensor changes? I think I read something about that being fixed at load time of loading the automation. I this really works that is of course the easiest.

That is really clever “hack”. Thanks! :slight_smile: I need to remember it because I’m pretty sure it could come handy in some weird situations, like the one I’m exploring here. :smiley:

1 Like

I think you’re thinking of trigger_variables

The docs state “The for template(s) will be evaluated when an entity changes as specified.”. I assume the “entity” in question is the one whose state is being monitored for the trigger… the phrasing is a bit ambiguous.

I somehow missed info in docs about for being able to get templates. Thanks, I’ll check if that works as it looks like! :slight_smile:

I think it could work. After reading example I tried to find something similar in the docs and came across Automation Trigger - Home Assistant example, where the last bit of code has for with template in it.

1 Like

Good to know templating is an option, I think I have discounted it myself before, seemingly for no reason then. Next time I need it I’ll give it a go.