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.
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.
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.
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
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.