Ya that’s a false negative bug in the syntax lib VSCode uses, there is a fix in place but VSCode hasn’t updated since it got merged.
@TheFes Thanks for this, I thought it would solve my scripting issue. I have concurrent automations needing to stash some text for another related automation. I therefore wanted the keys to be dynamic, however when I tried using this with dynamic variable names I found that it didn’t pass validation on startup.
Errors such as:
value should be a string for dictionary value @ data['actions'][0]['parallel'][0][0]['sequence'][3]['action']. Got None
and
invalid key: "{'review_id': None}"
Example:
variables:
review_id: "{{ trigger.payload_json['after']['id'] }}"
actions:
- event: set_variable
event_data:
key: {{ review_id }}
value: "{{ wait.trigger.payload_json['description'] }}"
set_timestamp: false
I tried single and double quoting the expression.
Does anyone know another way?
Thanks
This needs quotes (either double or single quotes) around the template
variables:
review_id: "{{ trigger.payload_json['after']['id'] }}"
actions:
- event: set_variable
event_data:
key: "{{ review_id }}"
value: "{{ wait.trigger.payload_json['description'] }}"
set_timestamp: false
Reference
Rule 1. You must surround single-line templates with double quotes (
"
) or single quotes ('
).
Thanks for the quick reply.
It’s only missing the quotes because that’s how the code ended. I got the first error with Single or double quotes - made no difference.
Finally i tried without any quotes which gave the second error message, and copied the code at that point.
sorry, but quotes doesn’t solve this issue. the validation sees that review_id is empty as the automation hasn’t fired, and disables the automation.
Thanks too,
I posted the code after trying all quote variations.
To be fair, I included removing the quotes in my testing because I was not confident they were required.
if that is always wrong, then I can remove it from my testing
Not required if the value is, or at least starts with,
an obvious string.
some_variable: cat
Required if the value is a template and begins with double opening braces.
some_variable: "{{ now() }}"
They are required because YAML also accepts json.
And json mappings also use {
and }
So without the quotes it will be interpreted as a (misformed) mapping by the YAML parser.
Thanks Both.
Coming back to this template sensor, are we concluding that it does not support key names using variables?
also, if you know of another way that might solve my issue
(Automation to publish and update notification on secondary MQTT trigger (frigate) - #9 by umbramalison)
It should work just fine, made a test script myself:
alias: Test 123
sequence:
- variables:
healthy_snacks:
fruit:
- apple
- banana
veggies:
- carrot
- cucumber
key: "{{ ['fruit', 'veggies'] | random }}"
- event: set_variable
event_data:
key: "{{ key }}"
value: "{{ healthy_snacks[key] | random }}"
Which resulted in
default_timestamp: false
log_events: false
variables:
veggies: cucumber
device_class: timestamp
friendly_name: Variables
The error you posted seems completely unrelated to this issue though, as it mentions an action
key.
Aha. Yep, that point lead me straight to it.
I only use the “global variable” in 3 places, set, consume, remove. I thought I understood the error message, but I was indeed conflating actions with events.
it appears to be working now that I fixed the real issue.
From: (with extra curlies)
- action: notify.residents
data:
title: "{{ msg_title }}"
message: "{{ msg_description }}"
data:
subtitle: "{{ state_attr('sensor.variables', 'variables')[{{ review_id }}] }}"
To:
- action: notify.residents
data:
title: "{{ msg_title }}"
message: "{{ msg_description }}"
data:
subtitle: "{{ state_attr('sensor.variables', 'variables')[review_id] }}"
Thanks for your help and for this solution to global variables.
I’ll update my thread with the complete solution.
What led you to believe you can nest one template inside another one?
{{ state_attr('sensor.variables', 'variables')[{{ review_id }}] }}
^^^^^^^^^^^^^^^
The post-mortem.
I certainly have not clicked with jinja. I was using {{ }} to denote variables to expand.
I have yet to understand how jinja identifies strings from variables inside an expression/template.
Let’s put it down to learning
Everything within the opening and closing double braces is understood to be Jinja2 code to be evaluated by the Jinja2 processor.
By embedding one template inside of another, the “inner template” isn’t a template but a literal string that the Jinja2 processor attempts to evaluate (and it cannot make sense of it).
EDIT
According to the automation you posted here, the nested template was used in notify.residents
action for title
and subtitle
. In other words, the error wasn’t even due to the event
action for TheFes’s Trigger-based Template Sensor.
Was it the automation’s trace that led you to believe the event
action was at fault?
Thanks for that explanation.
I didn’t appreciate the boundaries of Jinja2. Based on what you’ve just said Jinja2 wouldn’t expect to see any double braces, if HA only uses the double braces to identify code for the Jinja processor.
understanding what is HA and what is Jinja2 will help me lot going forward. Much appreciated.
Whenever you can spare the time (perhaps on a rainy Saturday morning), I recommend you review the Templating documentation.
Most computations in Home Assistant are performed with Jinja2 so it’s important to have a good understanding of its syntax and capabilities.
Is there a way to List out the contents of the Variable?
My Jinja is basic at best and much of this Template Sensor’s inner workings baffle me… Listing its contents nicely is way past my Jinja belt colour.
Some more varied examples of its Use Cases and the relevant Set / Get (state_attr(…) forms would also be a nice addition.
Thanks in hope.
Hi
Probably this has nothing to do with this template sensor but, how can I fire the set_variable event from a button-card?
type: custom:button-card
name: set x var
tap_action:
- event: set_variable
event_data:
key: x
value: 111
set_timestamp: false
This does not worked!
Thanks
You can’t (as far as I know), but you can write a script where you pass the same arguments and then trigger an event there.