I’ve done some research and think it has something to do with multi-line Jinja template syntax, but I cannot seem to figure out the correct spot to put single/double quotes and or (-) signs.
Can someone please point me in the right direction for correcting this?
Most standard cards don’t support Jinja2 templates. The linked example you posted won’t work unless you are using lovelace-gen. The author mentioned this important pre-requisite in a subsequent comment.
Subsequently, I noticed that Tesla post is from 2020 and that the Developer Template Tool specifically says “Templates are rendered using the Jinja2 template engine…”
So, I figured YAML mode and lovelace_gen is no longer necessary (and I’d prefer to use the UI for most of my dashboard designs). I’m able to get the desired result from the same code he shared in the Template Tool, but it doesn’t work in the card which is confusing.
Ok. This seems like something that should be implemented. But, I cannot help that.
In the meantime, as a workaround, is there a way to dynamically store the string created by the Jinja2 code (e.g. as an attribute of a dummie entity) that I could then reference in a simple template string for use anywhere Jinja2 is not yet implemented?
Alternatively, is there another non-standard card you’d recommend for this use case (i.e. a more feature rich Picture Element card)?
Yaml only loads on startup/reload. What you want is not possible without lovelace_gen. So your options are: Learn lovelace_gen, manually program everything by hand, or use the template editor to generate your yaml and copy/paste it into your lovelace-ui.yaml file.
@petro Thanks. I’ve been playing around with switching back and forth between the UI Editor (using the Raw configuration editor) and YAML mode (using the ui-lovelace.yaml), but must still be missing something in the configuration as I haven’t been able to get a dashboard to render in YAML mode.
Some follow up questions:
To be sure, do I just create a ui-lovelace.yaml from scratch or did I miss a step wherein HA would automatically create it (e.g. based on the existing UI cards) upon switching to YAML mode and restarting? So far, I tried to create it using the code from the Raw configuration editor.
I did see what you commented about YAML only being referenced on startup/reload. Does that mean that all templates used throughout HA are also loaded only at those times?
Is there talk about adding the features of lovelace_gen into the UI editor (or perhaps more specifically, certain cards)? I’m probably not asking that one quite right.
@123 OK, thank you for your help. I understand the general concept of Templating, but all I really need in this case is the string of characters it produces formatted in YAML. So, it seemed to me there might be a workaround (to a card not supporting templating). Maybe not.
You’re right, my original question is answered. Now I’m just trying to learn.
Yes, you create it from scratch. All yaml. Jinja can only be used in included files. You 100% cannot use the UI at all with lovelace_gen. Everything has to be done through files.
All templates are loaded at startup only. Templates are resolved when a state change occurs. The templates only listen to entity state change based on what’s found at startup. I.e. if a template uses entity xyz, it will only resolve when xyz changes state.
This will never happen. It’s not feasible. Lovelace_gen is not a card. It’s an integration that changes how yaml is loaded.
@petro Thanks. After reading this and messing around a bit more and saw that. So, it’s not as global of a change as I previously understood. I think I’ll keep a UISandbox dashboard to generate YAML that I can then copy/paste into the main YAML dashboard.
But, I cannot seem to get this approach to work with custom cards. That’s what produces the red errors. Does YAML only recognize standard (non custom) cards or is there something else I need to do for that?
Ah, thanks. I got it working now! The hybrid approach to dashboard configuration seems like a good way for me to learn more before I go all in on YAML mode exclusively.
@petro Now that I understand how to properly switch to YAML mode and have come to understand YAML syntax more, I’m trying to get lovelace_gen working for the original purpose of this post. I’ve carefully reviewed and checked all of the steps mentioned here ( thomasloven/hass-lovelace_gen: Improve the lovelace yaml parser for Home Assistant (github.com)) including setting up the custom component directory with the contents, switching to YAML mode, and including # lovelace_gen at the top of the referenced .yaml file (not ui-lovelace.yaml), but I"m still getting this error:
while scanning for the next token found character ‘%’ that cannot start any token in “/config/YAMLTesla.yaml”, line 6, column 2
I don’t think it’s a YAML syntax error because (1) I copied the raw code straight from the Tesla poster’s github and (2) as mentioned earlier, the same code pasted in the template editor produces usable YAML code.
Instead, I suspect something with my lovelace_gen configuration still isn’t right resulting in Jinja2 still not working on HA load.
What do you think should be my next steps to troubleshoot?