How to create a variable and use it in different places in an automation?

Hi,
I’ve got this automation —>

  • id: door_left_open
    alias: ‘Notify if the door is left open for 5 minutes’
    trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d0001bf91aa
    to: ‘on’
    for:
    minutes: 5
    action:
    • service: persistent_notification.create
      data:
      message: Door left open

Is it possible to set the number 5 as a variable, to use it in the alias+the for loop ?

Not in the way you’re thinking, but the concept is possible using a template trigger following the last updated attribute for your entity.

Or you could use a delay and condition in your action, there’s lots of creative ways to achieve it, depends on the circumstances which is best.

Thank you for your reply, I thought there would be a super obvious way, as I’ve seen these kind of config

 icon_template: >-
        {% set battery_level = states('sensor.mailbox_battery')|int('unknown') %}
        {% set battery_round = (battery_level|int / 10)|int * 10 %}
        {% if battery_level == 'unknown' %}
          mdi:battery-unknown
        {% else %}

So I thought (incorrectly!) that I would probably be able to set variables with “set” somewhere before my door automation.

I guess that another way would be to use AppDaemon, setting vars would become very easy.

Yeah, the problem is that not everything accepts templates, so you can’t use:

for:
  minutes: "{{template}}"

But you can replace the whole trigger with a template.