Configuring variables into templates in a blueprint

So, looking for more template help…
I’m dabbling in Blueprints again, and can’t find the documentation for this problem. My eyes have googled about 50 sites including a lot of other Blueprints, but I haven’t found it.
So…
In Blueprints you have the !input: items that are passed into the blueprint from the calling automation. These are very limited. So to do things like templates, you need to pass them to variables and you can use them there.
I am trying to find a way pull the current state (value) of an entity and use it as data to change an entity. I don’t know how to format the state statement.
How do I use a variable of the
input_number.lock_state
entity into something like this:
{{ ( states.input_number.lock_state.state) | int }}
and
states.input_number.lock_state.last_changed

Here’s what I’m re-creating inside a blueprint: (this is fine in the template checker)

      {% set tlast = as_timestamp(states.input_number.lock_state.last_changed) | int %}
      {% set tnow = as_timestamp(now())  | int %}
      {% if tlast < tnow - (5*60) %}
        0
      {% else %} 
        {{ ( states.input_number.lock_state.state ) | int }}
      {% endif %}

actual blueprint excerpts: (the question marks are that I tried dozens of things, but haven’t found the right one yet)

inputs:
  seq_status:
  name: Control Sequence Status Placeholder
  description: lock sequence number (set range to 0 to 32)
  selector:
    entity:
      domain: input_number

variables:
  seq_status_ent: !input 'seq_status'
  seq_status_last: {{ as_timestamp(states.???seq_status_ent???.last_changed) | int }}
  tnow: {{ as_timestamp(now()) | int }}
  seq_status_val: {{ ( states.???seq_status_ent???.state | int ) }}

action:
    # This one is always run every trigger as the watchdog.
- alias: "Reset seq number to 0 if timeout occurred"
  service: input_number.set_value
  data:
    entity_id: !input 'seq_status'
    value: >
      {% if seq_status_last < tnow - (5*60) %}
        0
      {% else %} 
        {{ seq_status_val }}
      {% endif %}

Or whatever will work here to set a 5 minute timeout to reset the sequence counter.

Any help would be appreciated…

Also if someone knows where HA has documented this or where I should suggest an edit so that it is, that would be helpful.

If only !inputs would work in jinja2…
(I know there are [FR]'s out there for this and I have voted them…)

FWIW, !input is meaningful to the YAML processor but the YAML processor doesn’t inspect the contents of Jinja2 templates (that’s a job handled by the Jinja2 interpreter).

So if you put !input inside a template, it doesn’t get converted by the YAML processor. As a result, the Jinja2 interpreter will see the raw !input statement but it has no meaning to Jinja2 so it will be reported as an error.

should just be

states(target_entity.entity_id)

Although I don’t use blueprints. But selectors usually are an object.

EDIT. but you need the variable using

target_entity: !input seq_status
1 Like

I do understand that, just pineing / whining there.

That doesn’t make sense. It’s in many blueprints. Here’s a random one using it:

2 Likes

If I take the question marks out it doesn’t find the entity
Blueprints must be (somewhat) weird iron…

there are no question marks…

I think you’re miss understanding 123. You can’t use !input directly in the template. So, the work around is to supply it to the yaml and use the variable in the template. Like mentioned above and in the example I posted.

(in my sample above, the question marks)
My problem is using the state entity variable to ‘build’ something that represents the actual state or the last_changed state.

I bet there is syntax but I can’t find it.

Dude, I gave you the answer already.

Here let me spoon feed you since you aren’t reading all the posts:

variables:
  seq_status_ent: !input 'seq_status'
  target_entity: !input seq_status
  seq_status_last: >
    {{ expand(target_entity.entity_id)[0].last_changed | as_timestamp }}
  tnow: "{{ as_timestamp(now()) | int }}"
  seq_status_val: "{{ ( states(target_entity.entity_id) | int ) }}"

EDIT: Just noticed the other ?? and you’re missing quotes on tempaltes. And you need the state object for the last changed above.

1 Like

I must be a complete idiot.

Target_entity is what I’m trying to pull out of the !input 'seq_status', which is impossible, or the variable assigned this entity, which is seq_status_ent, and that isn’t working.

This does not find the target_entity…
seq_status_val: {{ ( states.seq_status_ent.state | int ) }}

I have assumed it should, but I have some syntax error, need to concatenate, or something.

I do use seq_status_ent elsewhere in the blueprint so that is fine

Your errors are because you don’t have quotes around templates and some of the syntax is wrong. See previous post again, it has edits.

1 Like

That could change things, I will try that.

Logger: homeassistant.components.automation.keypad_5_button_cypher_to_turn_on_something
Source: components/automation/__init__.py:465
Integration: Automation (documentation, issues)
First occurred: 12:04:01 PM (1 occurrences)
Last logged: 12:04:01 PM

Error rendering variables: UndefinedError: 'str object' has no attribute 'entity_id'

I direct copied your suggestion, does this kind of variable have an entity_id component?
I found configuration variables variable listing but that does not list ‘entity_id’.

Is it supposed to be pulling that thru the entity via the !input-ed variable? Or what am I missing?

OK.
The key was the expand thing, but there is no entity_id attribute in input_number. So I played with a bunch of stuff and fell into leaving the attribute off completely, and that is working.

I’m looking thru the HA & jinja docs trying to understand the ‘expand’ tag so I actually understand this, but you pointed me in the right direction and I have it working now. Will plug it into my blueprint shortly.

If I don’t actually understand what this is exactly doing, I will be doomed to repeat it and will not be able to explain and defend it with others.

Thanks @petro !!

variables:
  seq_status_ent: !input seq_status
  seq_status_last: >
    {{ expand(seq_status_ent)[0].last_changed | as_timestamp }}
  seq_status_val: >
    {{ expand(seq_status_ent)[0].state }}
  tnow: "{{ as_timestamp(now()) }}"
2 Likes