I have a huge yaml file over 8000 lines of code for front end (it’s floor plan with control of basically everything I have in HA). I’d like to reduce the maintenance pain in the future and would like to ask if there is an easy way to reuse as much code in frontend yaml as possible. For example I have below code for a thermostat on picture element card, basically I repeat almost the same code for my other 12 thermostats, the only difference is the position (top, left, entity and title). I read for card-mod it is possible to use class, but here it’s just plain yaml code, is there a way to “template” it and apply with small changes to all of the same state-labels for thermostats?
As for reusing card-mod:
Main card mod thread - 1st post - link at the bottom - others - reusing a code
Now - some variants described there can be used for other places in frontend. So, that tutorial can give you a common idea which you may adapt for your needs.
Lovelace_gen is the best way to do reuse code in this setup.
I have the same situation, al though only with 5 thermostats.
I have made a single template for a thermostat and then I have a lovelace file that looks like this to add 5 thermostats.
I just realised that once the anchors and alias saved by frontend it’s revert to the full content (I’m stupid not to take a backup of the files), that’s sad as I now lost 3 hours work on this
WTH the YAML is not preserved when it’s saved
Hi thanks! I just didn’t realised that when I was working on it and I’m stupid not to save the working file with the anchors and alias - so all need to be done again.
I’m now thinking to switch to YAML mode but a bit concerned as UI editing is not possible, I have some simple views that make sense to edit in UI.
Does anybody know a good way to combine YAMLs with complex code and simple panels with UI (all in the same dashboard)?
Many thanks!
I read !include only works under yaml mode, I’m not sure if it’s a good idea for me to switch to yaml mode because for my rather complex dashboard there are certain things easier to manage with UI. Especially when I need to create new views for quick exploration UI mode comes handy. What’s your workflow?
It is YAML only.
I do not find it that hard to make a change and reload the dashboard.
Sometimes it fails to load and then I have to revert the changes or find the error to make it show again, but it is not that bad, I think.
Thanks! I’m sort of running out of ideas:
I do like the YAML mode where everything in config folder is versioned with GIT, and it supports !includes and anchors. However I have some concerns:
Every time when YAML is changed I need to reload manually (it’s not like in storage mode just save and you see result instantly)
There is no migration path if HA ditches YAML mode in the future (just saying), all files organised with include and anchors need to be resolved before pasting into HA UI (not sure how to do that). I wish there is an easy way to quickly switch between storage and YAML modes.
I do appreciate UI mode for build simple stuff like tile cards bubble card etc, but I must admit I applied many card-mod styling after so when the structure is there I find myself working with YAML more and more
With storage mode it also gets tricky, today I have a manual workflow to copy yaml from UI editor to yaml file for version control, but just learned that anchors will all be lost during the translation…
it is impossible to ditch YAML mode.
It would severely limit the options for users, because every single combination of YAML will have to be made in GUI and that is pretty much impossible and just trying to get there will require a huge workforce, which is not likely to happen in the near or distant future.
Of course there is the option to simply say to the user base that only the configuration the devs like are the ones that can be used, but then home assistant will be dead and a new HA will be forked from the GitHub repository. This is also highly unlikely. The devs like options and the GUI is more a necessity for the user base, than it is a wish from the devs.
I require a little change in the workflow, yes.
It is however the only way to truly reuse code, because all the other solutions have the limitation that the keys can not really be templated, only the values.
Lovelace_gen makes it possible to template the keys too.
The first obstacles is to wrap your head around the two times YAML parsing.
The first parsing will not know any values from the state machine. The second parsing, which is the normal one, will know it, so you need to keep that in mind at first.
Once you get the concept, then it is no problem.
Thanks for your help! I’m now convinced to move to YAML mode for the main dashboard.
What I plan to do (please let me know if it makes sense):
Keep the main dashboard in YAML and use !include to separate pop-up views (those are based on bubble card pop-ups)
Those separated views can be used to paste into HA for testing purpose, so it doesn’t use !include, but may contain anchors
Create a test dashboard in storage mode.
Add everything in. config folder in my git repository
So I’m trying to get the best from both world as much as possible.
Now the problem: I have just switched the main dashboard to YAML and copy paste my working dashboard to ui-lovelace.yaml. However it doesn’t load, only shows me a blank page. I then switch back to my old dashboard in storage mode, to my surprise all resources are gone! I tried to reinstall card-mod, button-card etc from HACS but doesn’t seem to get them back.
So now I have 2 broken dashboards which I have no idea how to fix…
Here you can find info about maintaining dashboard in yaml:
Probably it will help you.
Inside any yaml file you can use everything method which was proposed earlier - secrets, include, anchors etc, and most advanced - Lovelace_gen (not to mention decluttering card which can be used to encapsulate a card).
Lovelace_gen require these three lines somewhere in your configuration.yaml.
lovelace_gen:
lovelace:
mode: yaml
The mode key disables storage mode completely AFAIK.
Maybe there have been changes to this since I first started using it.
The last couple of years a lot have been changed in relation to dashboards, so I might be wrong now.