I’m using Owntracks to push battery data from my phone via MQTT to HA. I then created a template to push the mattery value to its own sensor so I can track the number over time. This works except on startup of HA when the initial sensor exists but has not attributes yet I get the error thrown:
Template variable error: ‘None’ has no attribute ‘attributes’ when rendering
The template I am using is:
gtab_battery:
friendly_name: GTab Battery
unit_of_measurement: "%"
value_template: "{{ state_attr('device_tracker.gtab', 'battery_level') }}"
icon_template: >-
{% set battery_level = states.device_tracker.gtab.attributes.battery_level | default(0) | int %}
{% set battery_round = (battery_level|int / 10)|int * 10 %}
{% if battery_round >= 100 %}
mdi:battery
{% elif battery_round > 0 %}
mdi:battery-{{ battery_round }}
{% else %}
mdi:battery-alert
{% endif %}
I did have an availability_template but as the sensor seems to get created by the addon and is empty at launch this didn’t stop the error.
Having read a lot of posts on the same error I am still struggling to find a way to stop them throwing an error and would really like a cleaner log file! Suggestions please.