Working with nested attributes in a template?

I’m trying to use a template to find if an attribute matches a particular string.
I can see how to get this to work except for nested values where after trying lots of different syntaxes I can’t get anything to work.

I’m trying to get an output of true, if the map name is “Kitchen-Lounge-Study”

entity vacuum.rockrobo

fan_speed_list:
  - min
  - medium
  - high
  - max
  - mop
battery_level: 99
battery_icon: 'mdi:battery-charging-100'
fan_speed: max
mainBrush: '297.2'
sideBrush: '197.2'
filter: '147.2'
sensor: '27.4'
currentCleanTime: '0.7'
currentCleanArea: '0.0'
cleanTime: '2.6'
cleanArea: '125.1'
cleanCount: 28
last_run_stats:
  startTime: 1613832725000
  endTime: 1613832768000
  duration: 43
  area: '0.0'
  errorCode: 0
  errorDescription: No error
  finishedFlag: false
bin_in_time: 402
last_bin_out: 1613772819004
last_bin_full: -1
last_loaded_map:
  name: Kitchen-Lounge-Study
  date: 1613832818379
state: docked
valetudo_state:
  id: 8
  name: Charging
friendly_name: rockrobo
supported_features: 10236

I have tried

{{is_state_attr("vacuum.rockrobo", "last_loaded_map.name", "Kitchen-Lounge-Study")}}

but this gives an error
I’m well aware that the problem seems to be that is_state_attr doesn’t seem to recognise the “.name” part of the code, as this syntax works perfectly with any of the non-nested attributes above.

Any hints?

Try this in the Developer Tools / Template editor:

{{state_attr('vacuum.rockrobo', 'last_loaded_map')["name"] == "Kitchen-Lounge-Study" }}
1 Like

Perfect and I can see the logic you’ve used.

Can you explain why my example doesn’t work though?

In this:

{{is_state_attr("vacuum.rockrobo", "last_loaded_map.name", "Kitchen-Lounge-Study")}}

The whole quoted string last_loaded_map.name is taken as a first level attribute rather than interpreting it as a nested attribute. It expects one attribute and has not been coded to interpret multi-levels.

Sorry for asking you, but what is the different form your solution and these template (i just found these in another thread)?
{{state_attr('vacuum.rockrobo', 'last_loaded_map').name == "Kitchen-Lounge-Study" }}
it is just for understanding because i’m a newbe in python Template

No difference. That format is also valid. There are some special cases where the square bracket notation has to be used though. e.g. in a restful sensor template with this resource:

value_template: "{{ value_json.something.something.value }}"

“value” has a special meaning in this case in home assistant (the non json return of the restful resource) so you have use this format to get to the actual path:

value_template: '{{ value_json.something.something["value"] }}'

Also, it’s a jinja template, not Python.

1 Like

Dot Notation: value_json.whatever
Bracket Notation: value_json["whatever"]

Dot notation looks neater but there are situations where it cannot be used. Tom_I described the exception where the key name is value. Other exceptions (where bracket notation should be used) are:

  • When the key name begins with a number.
    Use this:
    value_json["2021data"]
    not this:
    value_json.2021data
    There’s an example in the documentation for the case where the entity’s name begins with a number.
  • When the key name contains a space.
    Use this:
    value_json["temperature data"]
    not this:
    value_json.temperature data
    There’s an example in the documentation at the bottom of this page.
4 Likes