UndefinedError: 'mappingproxy object' has no attribute

Hi,

I’m using a template sensor to monitor a (hardware) sensor’s battery state:

platform: template
sensors:
  bat_fgms:
    value_template: '{% if states.binary_sensor.fibaro_system_fgms001_motion_sensor_sensor_17_0 %}
      {{ states.binary_sensor.fibaro_system_fgms001_motion_sensor_sensor_17_0.attributes.battery_level | int }}
    {% else %}
      n/a
    {% endif %}'
    friendly_name: 'Fibaro Motion Sensor'
    unit_of_measurement: '%'
    entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor_17_0

Despite checking if the state object exists, I still receive this error at HA startup:

[homeassistant.components.sensor.template] Could not render template Fibaro Motion Sensor: 
UndefinedError: 'mappingproxy object' has no attribute 'battery_level

How can I fix this?

Sebastian

1 Like

Nobody has an idea?

Sebastian

I have a similar issue that just started on my ipad and ipad mini but works fine on my iphone. I have the same config on all 3. If I test templates in the Developer tools and put the iphone template in it works but when I change the devicetracker name to my ipad it gets the error,

here is my sensor config:

- platform: template
  sensors:
    battery_iphone:
      unit_of_measurement: '%'
      value_template: >-
          {%- if states.device_tracker.tylers_iphone.attributes.battery %}
              {{ states.device_tracker.tylers_iphone.attributes.battery|round }}
          {% else %}
              {{ states.sensor.battery_iphone.state }}
          {%- endif %}
      icon_template: '{%- if is_state("sensor.battery_iphone", "unknown") %}mdi:battery-unknown{%- elif is_state_attr("device_tracker.tylers_iphone", "battery_status", "Charging") %}mdi:battery-charging{%- elif  states.device_tracker.tylers_iphone.attributes.battery <= 5 %}mdi:battery-outline{%- elif states.device_tracker.tylers_iphone.attributes.battery >= 95 %}mdi:battery{% else %}mdi:battery-{{(states.device_tracker.tylers_iphone.attributes.battery|float / 10)|round*10}}{%- endif %}'


- platform: template
  sensors:
    battery_ipad:
      unit_of_measurement: '%'
      value_template: >-
          {%- if states.device_tracker.tylers_ipad.attributes.battery %}
              {{ states.device_tracker.tylers_ipad.attributes.battery|round }}
          {% else %}
              {{ states.sensor.battery_iphone.state }}
          {%- endif %}
      icon_template: '{%- if is_state("sensor.battery_iphone", "unknown") %}mdi:battery-unknown{%- elif is_state_attr("device_tracker.tylers_ipad", "battery_status", "Charging") %}mdi:battery-charging{%- elif  states.device_tracker.tylers_ipad.attributes.battery <= 5 %}mdi:battery-outline{%- elif states.device_tracker.tylers_ipad.attributes.battery >= 95 %}mdi:battery{% else %}mdi:battery-{{(states.device_tracker.tylers_ipad.attributes.battery|float / 10)|round*10}}{%- endif %}'

- platform: template
  sensors:
    battery_ipad_mini:
      unit_of_measurement: '%'
      value_template: >-
          {%- if states.device_tracker.tylers_ipad_mini.attributes.battery %}
              {{ states.device_tracker.tylers_ipad_mini.attributes.battery|round }}
          {% else %}
              {{ states.sensor.battery_iphone.state }}
          {%- endif %}
      icon_template: '{%- if is_state("sensor.battery_iphone", "unknown") %}mdi:battery-unknown{%- elif is_state_attr("device_tracker.tylers_ipad_mini", "battery_status", "Charging") %}mdi:battery-charging{%- elif  states.device_tracker.tylers_ipad_mini.attributes.battery <= 5 %}mdi:battery-outline{%- elif states.device_tracker.tylers_ipad_mini.attributes.battery >= 95 %}mdi:battery{% else %}mdi:battery-{{(states.device_tracker.tylers_ipad_mini.attributes.battery|float / 10)|round*10}}{%- endif %}'      

and here are the errors:

[homeassistant.components.sensor.template] Could not render icon template battery_ipad: UndefinedError: ‘mappingproxy object’ has no attribute ‘battery’

[homeassistant.components.sensor.template] Could not render icon template battery_ipad_mini: UndefinedError: ‘mappingproxy object’ has no attribute ‘battery’

1 Like

Let me ask here too.
@sebk-666 @Tyler_Terzigni Did you guys found the solution to this problem?
I have similar errors in logs from other smart device and I have no idea how to solve it.

I wasn’t able to get rid of those warnings - I’m just ignoring them now.
Everything works as expected as soon as all the state objects are initialized.

Sebastian

This is what I switched to…

- platform: template
  sensors:
    battery_ipad:
      unit_of_measurement: '%'
      value_template: >-
          {%- if states.sensor.tylers_ipad_battery_level.state %}
              {{ states.sensor.tylers_ipad_battery_level.state|round }}
          {% else %}
              {{ states.sensor.tylers_ipad_battery_level.state }}
          {%- endif %}
      icon_template: >
          {% set battery_level = states.sensor.tylers_ipad_battery_level.state|default(0)|int %}
          {% set battery_round = (battery_level / 10) |int * 10 %}
          {% if battery_round >= 100 %}
            mdi:battery
          {% elif battery_round > 0 %}
            mdi:battery-{{ battery_round }}
          {% else %}
            mdi:battery-alert
          {% endif %}
1 Like