However, the above fails. YAML check out ok but when I reload templates, the sensor becomes unavailable and there’s an error in the logs. If I simply change to the following:-
In general, you can not truly nest attributes under other attributes in yaml. HA will reject it because it wants a flat dictionary. You could create a jinja library of all the attributes under one specific attribute, then create more templates to parse those attributes so each attribute has it’s own entity.
Thanks for confirming what I had begun to suspect. Strange though that in the several discussions about how to access nested attributes, no-one mentioned they don’t work in YAML and show them created nested exactly as I tried.
I guess I’ll proceed with the flat dictionary that seems to work.
The reason why I asked you to post your Template Sensor’s configuration is because I suspect you attempted to define the “nested attributes” using YAML. You need to use a template to define them.
The following screenshot shows the state and attributes of a Template Sensor. The value of its capacity attribute is a dictionary containing two keys: battery and units.
I’m afraid I did not see your original reply. Thanks for following it up, but I did actually post the exact YAML I was using in my attempts to create nested attributes. Yes YAML, which I now realise is not possible. Something of a surprise initially since I found several examples of it being used in the exact way I showed and with claims of it working - which is apparently impossible.
I’m not complaining about it not being possible, just exasperated at the ambiguity and apparent downright inaccuracy of what has previously been posted on this subject.
I did previously read the docs on Templates as you linked to, but found no mention of how to create nested attributes.
I understand now that it cannot be done directly in YAML and for my purposes that makes it less suitable, so I’ve used a flat dictionary of attributes and that’s working for what I need.
Thanks anyway for the offer of help. Much appreciated.
Ohh, that went over my head when I was looking at the docs, but you explained it very well with your example. Thanks for that. I learned something new.
I can understand your frustration. What probably hurt was searching for “nesting” when what you really wanted (but maybe didn’t know it) was how to set an attribute as a dictionary.
The only breadcrumbs are the docs for the state object here, and note this line:
state.attributes A dictionary with extra attributes related to the current state.
Which just tells us that attributes is a dictionary. You’d have to then know that a dictionary can store different object types inside it, one of those being another dictionary.
Then, as Taras already mentioned, the documentation (here) tells you that you need to use a template for each key that you define in the attributes dictionary.
So then you’re just left with figuring out a jinja template that will render as a simple dictionary.
Is it easy & clear? No. But if you can think of ways to make it better, you can always make suggestions for the docs. There’s a link at the bottom of each page. Honestly I’m not sure offhand how to make it better. But until it does get better, you won’t be the last one to ask the question.
Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which indicates to others that the topic has been solved. It helps other users find answers to similar questions.
Unfortunately, it’s not my OP cause I most certainly would. @UkenGB If you follow their advice, you’ll be able to “nest” attributes. It still requires a template string, but be sure to mark @123 post as a solution.
Please consider “bookmarking” my post above with the Solution tag. It will mark this topic as solved and help other users find answers to their questions about nested attributes.
Oh, I see you already tagged mmstano’s post as the Solution … even though he later thanked me for my explanation helping him learn something new.