Helping with trigger variables & inputs

You can, you just don’t have access to the state machine. trigger_variables.

That’s a blueprint only keyword

Basically I need the templated trigger in blueprints only - the trigger depends input variables passed into the blueprint.
For example, there is a variable specifying some “for” period - this variable is “input_number”, I need to get it’s state:

  1. I can get it’s state in variables section - but I cannot use this variable in trigger.
  2. I cannot use that “states(!input some_entity_id)” in trigger too.

no… you can, you just arn’t thinking about it correctly. All !input does is plop a target dict into a field.

variables:
  my_junk: !input xyz
  my_other_var: >
    {% set xyz = states(my_junk.entity_id) %}

Again, you can. You have to use trigger_variables using the method described in point 1.

I do not understand this code, particularly the 2nd line (with “set xyz”).
What I tried is (gives an error):

variables:
  INPUT_TIMEOUT: !input input_INPUT_TIMEOUT
  VALUE_TIMEOUT: "{{ states(INPUT_TIMEOUT) }}"
trigger:
  - platform: state
    entity_id: binary_sensor.some_sensor
    to: 'off'
    for:
      minutes: "{{VALUE_TIMEOUT}}"

or this:

    for:
      minutes: "{{states(INPUT_TIMEOUT)}}"

or this:

    for:
      minutes: "{{states(!input input_INPUT_TIMEOUT)}}"

or this:

trigger_variables:
  INPUT_TIMEOUT: !input input_INPUT_TIMEOUT
  VALUE_TIMEOUT: "{{ states(INPUT_TIMEOUT) }}"
trigger:
  - platform: state
    entity_id: binary_sensor.some_sensor
    to: 'off'
    for:
      minutes: "{{VALUE_TIMEOUT}}"

Can you post the errors please

That won’t work. variables is after triggers.

That won’t work because states machien is not available in template variables as i’ve said twice now.

Assuming the input is an entity_id target…

trigger_variables:
  INPUT_TIMEOUT: !input input_INPUT_TIMEOUT
trigger:
  - platform: state
    entity_id: binary_sensor.some_sensor
    to: 'off'
    for:
      minutes: "{{ states(INPUT_TIMEOUT.entity_id) }}"

You’re going to have to play around because I can’t tell what input is, only you know that.

But:

  1. You cannot use a variable in template since variables are initialized after trigger.
  2. You cannot use “!input” in template - do not know why, this code does not work:
  - platform: state
    entity_id: !input ....
    from:
      - ...
    to:
      - ...
    for:
      minutes: >-
        {{ states(!input xxxxxxxxxxxx) | int }}
1 Like

From my examples:

  1. If using variables from variables:
expected float for dictionary value @ data['for']['minutes']. Got None
  1. If using variables from trigger_variables:
invalid template (TemplateSyntaxError: unexpected char '!' at 9) for dictionary value @ data['trigger_variables']['VALUE_TIMEOUT']. Got None

This code gives an error:

Template variable error: 'str object' has no attribute 'entity_id' when rendering '{{ states(INPUT_TIMEOUT.entity_id) | int }}'

Can you just post your entire blueprint and I’ll fix it

Thank you very much!
Here is a short version:

blueprint:
  name: "common: Group update"
  description: ''
  domain: automation
  input:
    input_VALUE_GROUP_ID:
      name: "Group's entity_id"
    input_SENSOR_IS_UNAVAILABLE_PRESENT:
      selector:
        entity:
          domain: binary_sensor
    input_INPUT_WAIT_PERIOD_BEFORE_UPDATE:
      name: "Timeout before update"
      selector:
        entity:
          domain: input_number
    ...

variables:
  VALUE_GROUP_ID: !input input_VALUE_GROUP_ID
  ...

trigger_variables:
  INPUT_WAIT_PERIOD_BEFORE_UPDATE: !input input_INPUT_WAIT_PERIOD_BEFORE_UPDATE

trigger:
  - platform: state
    entity_id: !input input_SENSOR_IS_UNAVAILABLE_PRESENT
    from:
      - "on"
      - "off"
    to:
      - "on"
      - "off"
    for: #'00:03:00'
      # minutes: "{{ states(!input input_INPUT_WAIT_PERIOD_BEFORE_UPDATE) | int }}"
      # minutes: "{{ states(INPUT_WAIT_PERIOD_BEFORE_UPDATE.entity_id) | int }}"
      minutes: "{{ states(INPUT_WAIT_PERIOD_BEFORE_UPDATE) | int }}"

action:
...

mode: restart

does this produce an error? If yes, then you’ll have to make a feature request for !input to work in trigger_variables. If no…

You have to figure out what object is created from that selector by looking at the resulting automation.

It should be either this

      minutes: "{{ states(INPUT_WAIT_PERIOD_BEFORE_UPDATE) | int }}"

or

      minutes: "{{ states(INPUT_WAIT_PERIOD_BEFORE_UPDATE.entity_id) | int }}"

depending on if it’s a full selector target or not.

FWIW, I don’t see how this would require a feature request and it should work out of the box because all !input does is place the input into the automation when generating.

The value of input_INPUT_WAIT_PERIOD_BEFORE_UPDATE is “input_number.some_value”.
As for a value of INPUT_WAIT_PERIOD_BEFORE_UPDATE - when it was in variables it was the same value; I do not know if it is same when it was moved to trigger_variables.

This code gives an error only when triggered (state is changed off->on):
Template variable error: 'str object' has no attribute 'entity_id' when rendering '{{ states(INPUT_WAIT_PERIOD_BEFORE_UPDATE.entity_id) | int }}'

trigger_variables:
  INPUT_WAIT_PERIOD_BEFORE_UPDATE: !input input_INPUT_WAIT_PERIOD_BEFORE_UPDATE
trigger:
    ...
    for:
      minutes: "{{ states(INPUT_WAIT_PERIOD_BEFORE_UPDATE.entity_id) | int }}"

This code gives no errors & is not triggered (does not work):

trigger_variables:
  INPUT_WAIT_PERIOD_BEFORE_UPDATE: !input input_INPUT_WAIT_PERIOD_BEFORE_UPDATE
trigger:
    ...
    for:
      minutes: "{{ states(INPUT_WAIT_PERIOD_BEFORE_UPDATE) | int }}"

This code gives an error on HA startup:
Error rendering trigger variables: TemplateError: str: Use of 'states' is not supported in limited templates

trigger_variables:
  INPUT_WAIT_PERIOD_BEFORE_UPDATE: !input input_INPUT_WAIT_PERIOD_BEFORE_UPDATE
  VALUE_WAIT_PERIOD_BEFORE_UPDATE: "{{states(INPUT_WAIT_PERIOD_BEFORE_UPDATE)}}"
trigger:
    ...
    for:
      minutes: "{{ VALUE_WAIT_PERIOD_BEFORE_UPDATE | int }}"

That’s the only correct one if INPUT_WAIT_PERIOD_BEFORE_UPDATE is the entity_id.

FYI, you dont need | int. You aren’t using it in the template and the output already infers the type.

That is what I wrote above:

Now I cannot understand why it does not work…


Yes, I got your point!


Btw, this also gives an error on startup:
expected float for dictionary value @ data['for']['minutes']. Got None

    for:
      minutes: "{{ states(!input input_INPUT_WAIT_PERIOD_BEFORE_UPDATE) | int }}"

Seems that “!input” is not allowed here.


Just in case I note that I restart HA after every change of the blueprint.

!input is just a yaml include but slightly different. It’ll never work in templates like that. You have to provide it to an entire field before you can use it. This is why we are using trigger_varaibles as it’s the only thing that resolves prior to a trigger.

They only resolve once, you should see what the result is when you look at the trace in the automation. Put in a second trigger to see the trace and see what the trigger_variables contain.

Petro, this is a working variant:

trigger_variables:
  INPUT_WAIT_PERIOD_BEFORE_UPDATE: !input input_INPUT_WAIT_PERIOD_BEFORE_UPDATE
trigger:
    ...
    for:
      minutes: "{{ states(INPUT_WAIT_PERIOD_BEFORE_UPDATE) | int }}"

I found out why I considered this code as non-working - I just switched off notifications via Telegram earlier: no notifications → doesn’t work. Such a stupidity)))
Thank you VERY MUCH for helping!

1 Like