Hello
I have specific sensor which collects a lot of data in its attributes which are json objects
sensor.doorwindow_counter.attributes.counter.obj1.json_data
sensor.doorwindow_counter.attributes.counter.obj2.json_data
etc
where json_data is for example
{
"count" : 0,
"time_reset": ...,
"time_last": ....
}
the point is, that objX may not exist at time of filling another template which references those data. See count attribute in template below
- platform: template
sensors:
child_room_window:
friendly_name: "Child Room Window"
device_class: "window"
attribute_templates:
battery: >-
{{ state_attr('binary_sensor.child_room_window_battery','battery') }}
tilt: >-
{{ state_attr('binary_sensor.child_room_window_tilt','tilt') }}
count: >-
{{ state_attr('sensor.doorwindow_counter','counters')['child_room_window_state']['count'] }}
It will fail if sensor.doorwindow_counter.attributes.counters.child_room_window_state
doesn’t exist.
Inexistence of count attribut is not critical, it can be suppresed by default(0).
But how can I suppress inexistence of the the first level key in counters object?
probably I could check existence first, then decide if go deeper or not. But in case of yet deeper structure it would have code havoc. Is there a nice way how to cope whith such situation?