Template sensor causes false warning at HA restart - timing issue?

Hi,

my zwave thermostats have an attribute battery_level that I would like to export to InfluxDB and graph it.
So I created a template sensor for each of the thermostats like this:

sensors:
  bat_livingroom:
    value_template: '{{ states.climate.danfoss_z_thermostat_014g0013_heating_1_4_1.attributes.battery_level }}'
    friendly_name: 'Battery Level LR'
    unit_of_measurement: '%'
    entity_id: climate.danfoss_z_thermostat_014g0013_heating_1_4_1

When I start HA, I get a warning message for each of the template sensors:

16-11-25 22:00:56 WARNING (MainThread) [homeassistant.components.sensor.template] UndefinedError: 'None' has no attribute 'attributes'

Could it be that at the time the sensors are created, the states object is not yet fully “populated”?
The sensors show up in the UI as expected, though.
I’m just curious about the warning message.

Sebastian

U have to wait a bit Till the zwave Network has finished to read out all values …it will be shown after a while. Are u also using grafana ?

Yes, I’m also using Grafana.
As I said, the sensor works fine, I’m just wondering about the warning message.

Sebastian

You can fix that with an if statement. Use the template dev tool to debug, but it’ll be something like:

'{% if states.climate.danfoss_z_thermostat_014g0013_heating_1_4_1.attributes.battery_level %}
  {{ states.climate.danfoss_z_thermostat_014g0013_heating_1_4_1.attributes.battery_level }}
{% else %}
  0
{% endif %}'

Thanks, I think that would work - I’d still keep it as it is, since that’s easier to read.
As long as everything works, I can live with the warnings if noone wants to fix this :wink:

Sebastian