Blueprint: cannot create an automation with a trigger "states is equal to XXX for YYY minutes"

Fighting with using a template for a trigger “states is equal to XXX for YYY minutes”.
Here is a working automation:

- alias: test0
  description: ''
  trigger:
  - platform: state
    entity_id: input_boolean.test_boolean
    to: 'off'
    for:
      minutes: '{{states(''input_number.wait_period_before_alarm'') | int}}'
  condition: []
  action:
  - service: notify.telegram_chat_XXXXXXX
    data:
      message: OFF for {{states('input_number.wait_period_before_alarm')|int}} minutes
  mode: single

It triggers if:

  • "input_boolean.test_boolean" changed to OFF
  • after period of time specified by "input_number.wait_period_before_alarm".

The output (message in Telegram) is:
OFF for 3 minutes

Now I am trying to create a blueprint:

blueprint:
  name: "common: Entity - problem or OK (long period)"
  description: ''
  domain: automation
  input:
    input_ENTITY_NAME:
      name: "Entity's name"
    input_ENTITY_STATUS:
      name: "Entity's sensor"
    input_LONG_PERIOD:
      name: "Long period"
      selector:
        entity:
          domain: input_number

variables:
  ENTITY_NAME: !input input_ENTITY_NAME
  LONG_PERIOD: !input input_LONG_PERIOD
  LONG_PERIOD_VALUE: "{{ states(LONG_PERIOD) | int }}"

trigger:
  - platform: state
    entity_id: !input input_ENTITY_STATUS
    to: 'off'
    for:
      hours: 0
      minutes: "{{ 3 | int }}"   #just for testing
      seconds: 0

action:
  - service: notify.telegram_chat_XXXXXXXXXX
    data:
      message: "{{ENTITY_NAME}}: OFF for {{LONG_PERIOD_VALUE}} minutes"

mode: single

Here is an automation:

- alias: testXXX
  description: ''
  use_blueprint:
    path: common_entity_problem_long.yaml
    input:
      input_ENTITY_NAME: FLAG
      input_ENTITY_STATUS: input_boolean.test_boolean
      input_LONG_PERIOD: input_number.wait_period_before_alarm

The automation works, here is an output:
FLAG: OFF for 3 minutes
Note that the blueprint has a “hardcoded” value “3” for the period.
But none of these variants does not work:

    for:
      minutes: "{{ LONG_PERIOD_VALUE }}"
      minutes: "{{ states(LONG_PERIOD) | int }}"
      minutes: "{{ states(!input input_LONG_PERIOD) | int }}"

These variants work:

    for:
      minutes: "{{ 3 | int }}"
      minutes: "{{ states('input_number.wait_period_before_alarm') | int }}"

Why at least 2 first variants do not work?

I believed that I can use variables in templates.
Here it does not work…

Tried to simplify.
Here is a working automation without a blueprint:

- alias: test
  description: ''
  trigger:
  - platform: state
    entity_id: input_boolean.test_boolean
    to: 'off'
    for: '0:{{3}}:0'
  condition: []
  action:
  - service: notify.telegram_chat_{{CHAT_TYPE}}
    data:
      message: '{{ENTITY_NAME}}: {{PROBLEM_VALUE}} (for {{LONG_PERIOD_VALUE}} minutes)'
  variables:
    CHAT_TYPE: important
    ENTITY_NAME: FLAG
    PROBLEM_VALUE: 'OFF'
    LONG_PERIOD_VALUE: '{{ 3 | int }}'
  mode: single

But this automation does not work:

- alias: test
  description: ''
  trigger:
  - platform: state
    entity_id: input_boolean.test_boolean
    to: 'off'
    for: '0:{{LONG_PERIOD_VALUE}}:0'
  condition: []
  action:
  - service: notify.telegram_chat_{{CHAT_TYPE}}
    data:
      message: '{{ENTITY_NAME}}: {{PROBLEM_VALUE}} (for {{LONG_PERIOD_VALUE}} minutes)'
  variables:
    CHAT_TYPE: important
    ENTITY_NAME: FLAG
    PROBLEM_VALUE: 'OFF'
    LONG_PERIOD_VALUE: '{{ 3 | int }}'
  mode: single

There is something special about "variables" which prevents using it for the "for" section.
But "variables" do work in the "action" section.

You can’t use variables in a templated trigger.

FWIW, I had created a Feature Request to support variables in Template Triggers.

I realize you are using a State Trigger but the limitation appears to be the same; it doesn’t accept variables in a trigger’s template.

1 Like

Thank you for raising the issue.