mayker
(maykar (pronounced "maker" with a southern accent))
1010
Sorry, was running to work and had to get my yaml mode dev install running.
There were a few spots that required the - to clear spaces. This works for me with no spaces and no errors:
test_template: >-
{%- set mapper = {'1':'one','2':'two','3':'three','4':'four','5':'five','6':'six',
'7':'seven','8':'eight','9':'nine','10':'ten','11':'eleven','12':'twelve'} -%}
{% set digit = now().strftime('%-I') %}
{%- set word = mapper[digit] -%}
{%- if states('sun.sun') == 'above_horizon' -%}mdi:clock-time-{{word}}
{%- else -%}mdi:clock-time-{{word}}-outline
{% endif %}
You can see this in the template tool as well, not entirely sure why whitespace would be added to some of those portions, but lets just call it Jinja magic (since I’m still waking up and can’t bring myself to research it more).
o wow, I am so stumped… Never ever realized we could do this in the template editor by adding the outer quotes…
And here am I continuously refreshing my Lovelace config, for the test_template to read the newly edited template…
still, its not identical:
"{% set mapper = {'1':'one','2':'two','3':'three','4':'four','5':'five',
'6':'six','7':'seven','8':'eight','9':'nine','10':'ten','11':'eleven',
'12':'twelve'} %}
{%- set digit = now().strftime('%-I') %}
{%- set word = mapper[digit] %}
{%- if states('sun.sun') == 'above_horizon' %}mdi:clock-time-{{word}}
{%- else %}mdi:clock-time-{{word}}-outline
{% endif %}"
and yet throws the aforementioned error in inspector again. After that, nothing helps. De-whitespacing the mapper or even completely whipping the custom-header settings to all defaults won’t make the error disappear. And the CH re-appear.
Only a restart of the system restores the correct Custom Header. This is browser independent, Chrome, Safari and Firefox show identically. Even the new Mac HA app
mayker
(maykar (pronounced "maker" with a southern accent))
1012
Indents again?
{% set mapper = {'1':'one','2':'two','3':'three','4':'four','5':'five',
'6':'six','7':'seven','8':'eight','9':'nine','10':'ten','11':'eleven',
'12':'twelve'} %}
{%- set digit = now().strftime('%-I') %}
{%- set word = mapper[digit] %}
{%- if states('sun.sun') == 'above_horizon' %}mdi:clock-time-{{word}}
{%- else %}mdi:clock-time-{{word}}-outline
{% endif %}
well yeah, probably, I was merely testing the testing capabilities of the test_template
seems it still doesnt parse the same way as the HA template editor
reproducing my the ‘bug’:
start with
test_template: >-
{% set mapper = {'1':'one','2':'two','3':'three','4':'four','5':'five','6':'six','7':'seven','8':'eight','9':'nine','10':'ten','11':'eleven','12':'twelve'} %}
{%- set digit = now().strftime('%-I') %}
{%- set word = mapper[digit] %}
{%- if states('sun.sun') == 'above_horizon' %}mdi:clock-time-{{word}}
{%- else %}mdi:clock-time-{{word}}-outline
{% endif %}
all is fine test template showing the correct output, like your post above
edit the template to include the indents and the inspector chokes on this
after that, no way to repair the CH, but to restart the instance.
Mind you, this is only using the test_template, not even a template used in the CH config.
mayker
(maykar (pronounced "maker" with a southern accent))
1014
Yup, just the quirks of using Jinja from yaml in Lovelace. Watch the indents. test_template renders exactly the same as any other template in Lovelace.
mayker
(maykar (pronounced "maker" with a southern accent))
1015
Also, why not drop the dict and use an array instead? That way you don’t need to map at all. Add ‘zero’ (or anything really, empty string works too) as the first item and then you can just use the hour as the array’s index:
Edited again to make it even shorter:
{% set word = ['','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve'] %}
{%- set index = now().strftime('%-I') | int %}mdi:clock-time-{{word[index]}}
{{- '-outline' if states('sun.sun') != 'above_horizon' }}
ha, yes! was looking to do so, you beat me to it while I was still struggling and awaiting the restart… wow, thanks!
how come then CH (not the test template apparently) throws the error while the template editor parses it perfectly?
If this is a Lovelace quirk, how to report on that, without being told I am using a custom integration for which no support will be given. Surely this is a bug.
Also, the fact that the system needs a restart while a simple template issue in the CH config errors isnt as it should be? Seems I have bumped into 2 issues that need looking after/into.
all in all this is a noteworthy exercise with a nice outcome:
will see if I can fix it so it rounds to the next or previous hour, depending on the minutes being below or above 30
trying:
{% set word = ['','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve'] %}
{% set addition = 0 if now().strftime('%-M')|int <= 30 else 1 %}
{%- set index = now().strftime('%-I')|int + addition %}mdi:clock-time-{{word[index]}}
{{- '-outline' if states('sun.sun') != 'above_horizon' }}
mayker
(maykar (pronounced "maker" with a southern accent))
1017
Jinja isn’t really supported in Lovelace, so it’s not really a bug, it’s a quirk of an unsupported feature and if reported I imagine that’s exactly what they would say about the custom addon. Keep in mind that what this is doing is converting the config from yaml to a string (which can have quirks) and sending that to the backend to be rendered as jinja. If the backend doesn’t get what it expects, you’ll get an error (usually not very helpful ones either), but none of this is the fault of HA devs. It’s my fault for adding this into CH.
You shouldn’t need to restart to clear the error, editing the config and refreshing with ctrl+shift+r should do the trick. If it doesn’t just use the ?disable_ch trick after editing the config and reload Lovelace from the top right menu.
I got those errors too when using the bad Jinja, but only had to refresh the page after making the change.
as said, not so here. was 100% reproducible.
Didnt try your ?disable trick though, will do so next time this arises. Thanks for your support, much appreciated. Love CH!
1 Like
mayker
(maykar (pronounced "maker" with a southern accent))
1019
Quick tip if you’re going to round like that, add another ‘one’ as the last item in the array. That way if it’s 12 and it rounds up to 13 it will still be ‘one’. You could add logic to make index equal 1 if it is currently 13, but just adding ‘one’ to the end of the array seems cleaner.
{% set word = [0,'one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','one'] %}
{%- set hour = now().strftime('%-I')|int %}{% set minute = now().strftime('%-M')|int %}
{%- set index = hour if minute <= 30 else hour + 1 %}mdi:clock-time-{{word[index]}}
{{- '-outline' if states('sun.sun') != 'above_horizon' }}
Also, in the next release I think I’ll be adding logic for templates that fail. You’ll still get an error in the logs, but I’ll make CH disable itself when it happens so the default header is still available.
ok, re-iteration on an earlier discussion we had, concerning CH on multiple dashboards.
Ive made my own shortcut menu bar, and have grown so accustomed to its ability to span all dashboards, Ive been trying to get my head around how the CH menu bar could do this. I cant seem to find that ability, though it really is an awesome feature.
Am I overlooking this option completely, or is it really not possible yet.
have a look what I am trying to say:
is my own shortcut menu bar, with buttons pointing to the various paths spanning my 3 dashboards. With some nifty templating, ive been able to even display depending on admin or not, and several modes or not. That is all possible too in CH of course.
Yet, the window.location.pathname used in my JS templates in the button card make the dashboard crossovers possible.
Would this be a feature available in CH too (now, or as a future addition)?
mayker
(maykar (pronounced "maker" with a southern accent))
1025
CH is way too feature bloated as it is. I’ve considered it, but adding tabs/buttons that don’t already exist in the default header isn’t something that I’d add.
by the above, you mean the tabs per dashboard right? By which the answer to the request for cross dashboard tabs has been given…
I understand what you’re saying, CH has a lot of magic options dont want to take away from that…
mayker
(maykar (pronounced "maker" with a southern accent))
1027
Exactly. There have been a few other feature requests for things that would interact with other dashboards, like using tabs in other dashboards for default tab, etc. Unfortunately, this causes more problems than it solves since each dashboard is it’s own Lovelace instance with it’s own config. This opens possibilities of endless loops of redirects and other fun bugs/unwanted behavior.
Just more trouble than it’s worth, especially since HA already has features like setting a default dashboard per device, admin only dashboards, the ability to hide dashboards from the sidebar, and I’m sure more will come with time.
The problem with many of HAs integrated solutions is that they just save the settings in the users browser, not in a users profile, so if I move bewteen browser, clear some local data on a tablet etc. the setting are lost.
They are also not very flexible. Say I want a special dashboard (with multiple tabs) for a certain user, no matter where that user is logging in from, ant the user should only have access to that dashboard (but to all the tabs inside it). And no other users should have access to it (except admin). Such a basic thing is just not feasible to set up today.
But that is just a rant, and not really constructive feedback for Custom Header (which is great for what it does, it just does not solve all problems). So sorry about that.
mayker
(maykar (pronounced "maker" with a southern accent))
1029
I agree and this might be a good feature request for HA itself, but it’s also a good idea for another custom project (just not this one).
It wouldn’t be difficult to create a custom “card” that would use JS templates (Jinja would be overkill) in order to redirect to any URL on certain conditions. I haven’t done too much research into this and it might already exist or be possible with a feature on another custom project.
The ultimate issue with things like this is there is the possibility of users accidentally creating conditions on two dashboards that would redirect from one, back to the other, and then continuously looping between the two. Could also create a scenario where one dashboard would be inaccessible as the redirect would always be active, so every time you try to visit it you get redirected away. So some sort of protection against such things would be needed. Whether that is storing variables in localStorage or otherwise.