home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
72.8k stars 30.5k forks source link

`trigger_variables` cannot be used in template trigger `for` key #120191

Open ondras12345 opened 4 months ago

ondras12345 commented 4 months ago

The problem

Template trigger documentation shows that templates can be used in the for key:

automation:
  trigger:
    - platform: template
      value_template: "{{ is_state('device_tracker.paulus', 'home') }}"
      for:
        minutes: "{{ states('input_number.minutes')|int(0) }}"

So, I tried to use a variable defined in trigger_variables:

automation:
  trigger_variables:
    entity: binary_sensor.some_entity
    delay: 1
  trigger:
    - platform: template
      value_template: '{{ is_state(entity, "on") }}'
      for:
        seconds: "{{ delay }}"
  condition: []
  action:
    - service: notify.persistent_notification
      data:
        message: test1
  mode: single

But I get this in my log:

2024-06-22 23:03:48.142 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'delay' is undefined when rendering '{{ delay }}'
2024-06-22 23:03:48.144 ERROR (MainThread) [homeassistant.components.template.trigger] Error rendering 'test1' for template: expected float for dictionary value @ data['seconds']

and the automation does not work. If I replace seconds: "{{ delay }}"' with seconds: 1 (or seconds: "{{ 1 }}"), it works as expected. Templates in value_template can use the entity variable without problems.

What version of Home Assistant Core has the issue?

core-2024.6.2

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Template

Link to integration documentation on our website

https://www.home-assistant.io/integrations/template/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

home-assistant[bot] commented 4 months ago

Hey there @phracturedblue, @tetienne, @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (template) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `template` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign template` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


template documentation template source (message by IssueLinks)

issue-triage-workflows[bot] commented 1 month ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

ondras12345 commented 1 month ago

Still an issue on 2024.9.0.