First time trying a template

Trying to start playing with templates and I have, what I assume, is a stupid question :grinning:

If I use this:

"{{ (( now() - trigger.from_state.last_changed).seconds) > 3 }}"

as a CONDITION, do I have to define the “trigger” or does HA use the same trigger from the automation it’s in automatically?

I was afraid that would be the case.
Why isn’t this working then?

alias: Test2
description: ""
trigger:
  - platform: state
    entity_id: switch.phone_switch
    to: "off"
condition:
  - condition: template
    value_template: "\"{{ (( now() - trigger.from_state.last_changed).seconds) > 3 }}\""
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.virtual_switch_tester
mode: single

I’m trying to turn on the switch.virtual_switch_tester when the switch.phone_switch has been off for 3 seconds.

I tried the change and now it seems to be ignoring the condition all together. Once the switch turns off, the virtual switch turns on.

alias: Test2
description: ""
trigger:
  - platform: state
    entity_id: switch.phone_switch
    to: "off"
condition:
  - condition: template
    value_template: "{{ (now() - trigger.from_state.last_changed).seconds > 13 }}"
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.virtual_switch_tester
mode: single

I changed the time to 13 seconds

Just to clarify, your change didn’t make it skip the condition, I noticed the "\ at the beginning and end, deleted them with the original script and the virtual switch comes on when the phone switch goes off.

Ok, I see now that I’m getting this backwards.
So the state.last_changed doesn’t include the triggered action I’m guessing.
Right now, if the phone switch is on for at least 13 seconds, then I turn it off, the virtual switch goes on. I tried this with different times and it worked when it should and didn’t when it shouldn’t.
So what would I do to get what I wanted? Which is when the phone switch gets turned off, 13 seconds later the virtual switch turns on?

Can you clarify your goal? There are at least 2 ways your statement above can be interpreted.

Option 1: The switch should turn off any time the phone switch turns off, just 13 second later:

alias: Test2
description: ""
trigger:
  - platform: state
    entity_id: switch.phone_switch
    to: "off"
condition: []
action:
  - delay: 13
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.virtual_switch_tester
mode: single

Option 2: The phone switch need to remain off for 13 seconds then the switch is told to turn off:

alias: Test2
description: ""
trigger:
  - platform: state
    entity_id: switch.phone_switch
    to: "off"
    for: "00:00:13"
condition: []
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.virtual_switch_tester
mode: single

Something else…?

I didn’t mean to say your change did nothing if that’s the way you took it. Just giving facts.

I never mentioned an error message so not sure what you mean. Irrelevant either way.

My goal is to learn templates so your examples aren’t useful at the moment and I’ve already done it that way. I’m just learning HA and trying different things on a “test (or lab) setup”.

Ok, so it doesn’t include the triggered action, good to know. Is there a way to make it, it being trigger.from_state.last_changed, include the state change in the trigger of the same automation?
Is there another Template that can be used to do what I’m trying?

The reason this fails is an error in logic. The trigger to off occurs immediately after the phone switch changes to off. Because of that, you condition logic will never pass, e.g. it will never have a last changed 13 seconds ago as it’s evaluated immediately after turning off. You would need either:

A wait for condition,

Or change the trigger to template and include checking the state is off in what you now have in value_condition. I think this method is what you are inferring when you say “a new way to do things”. A template that confirms the entity state is off, and that the last changed was > 13 seconds ago.

That’s incorrect. The template is using the values from the previous state change… which could have been days ago. However, I truly doubt that the behavior that will be elicited by that condition is actually what OP wants.

1 Like

?

The trigger variable is an object created at the moment your automation is triggered. It contains information about the entity (switch.phone_switch) responsible for triggering your automation’s State Trigger, specifically information about the entity’s state before (from_state) and after (to_state) it changed.

References

Available trigger data
State Object

Ah, thanks, that makes sense. I guess the logic error is mine! :slight_smile: