Nested attributes

I’ve created a template sensor with some basic attributes and all is good. However, I’d like to nest some attributes like this:-

    attributes:
      code: 1234
      capacity:
        battery: 34
        units: kWh

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:-

    attributes:
      code: 1234
      capacity: 12
      battery: 34
      units: kWh

so all just basic attributes, HA is happy again.

I’ve been searching for ages, but all that I can find indicates that how I nest them in the first example is correct, but HA refuses to play ball.

What’s the secret recipe to create a sensor template with nested attributes?

Post your Template Sensor’s configuration.

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.

Thanks again.

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.

Here’s the Template Sensor’s configuration:

  - name: Example ABC123
    state: "{{ now() | as_local }}"
    attributes:
      code: 1234
      capacity: "{{ dict(battery=34, units='kWh') }}"

The following screenshot shows how to reference the values.

If you post your Template Sensor’s configuration, I can show you how to adapt it to meet your needs.

The documentation indicates it must be a template.

2 Likes

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.

Post links to these examples.

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.

1 Like

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.

Yes you can, you just can’t use yaml.

do this

        attributes:
          code: 1234
          capacity: "{{ {'battery', 34, 'units': 'kWh'} }}"

The config validator for the yaml, needs the result of each attribute to be a single result or a template.

I appreciate it, I actually gave Taras props for correcting me in an earlier comment and teaching me up.

You’re welcome! Glad to hear it helped.

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.

Oops! My mistake. I overlooked the fact that more than one person in this topic had encountered the same issue.

Anyways, I’m still glad to hear the explanation helped you.

1 Like

Sorry, I didn’t read the whole thread

I would if I’d bookmarked them. :slightly_smiling_face:

However, since they failed to work I discarded them.

1 Like

That makes sense.

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.