Startup Template Error

I have a template sensor that is failing on startup now (core-2021.12.9 - was fine on 12.8). It used to recover, but now continues to be unavailable.

Is there a simple fix? Should there be a built-in mechanism to manage startup times of sensors? Perhaps something as simple as a ‘delay’ setting that could be used, or an ‘after’ setting (like systemd).

      sensor_fault:
        friendly_name: "Sensor Fault"
        value_template: >-
          {{ states.sensor  
            | selectattr('attributes.unit_of_measurement', '==', '°C')
            | selectattr('attributes.sensor type', '==', 'LYWSDCGQ')
            | selectattr('attributes.last median of', '<', 5)
            | list | count > 0 }}
2022-01-14 08:11:28 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: 'mappingproxy object' has no attribute 'last median of'') while processing template 'Template("{{ states.sensor  
  | selectattr('attributes.unit_of_measurement', '==', '°C')
  | selectattr('attributes.sensor type', '==', 'LYWSDCGQ')
  | selectattr('attributes.last median of', '<', 5)
  | list | count > 0 }}")' for attribute '_state' in entity 'binary_sensor.sensor_fault'

[Edit]
I have effectively done the same in Node-RED so my next step is to create the sensor from there, but it feels wrong :frowning:

msg.status = msg.data.filter(function(data){
    return (data.attributes.device_class == "temperature") && ((data.attributes.last_median_of < 5) || (Date.parse(data.last_updated) + 300000) < Date.now());
}).length > 0;

return msg;

I use an input_boolean that ist set on HA start.

input_boolean:
  ha_started:
    initial: false

automation:
  - alias: HA Start automation
    initial_state: True
    trigger:
      platform: homeassistant
      event: start
    action:
      service: input_boolean.turn_on
      entity_id: input_boolean.ha_started

and in some sensors that show errors on startup

        value_template: >
          {% if states('input_boolean.ha_started') == 'on' %}
            ...
          {% else %}
            unavailable
          {% endif %} 
1 Like

That is an excellent idea.

It still has the issue that you have no idea which sensor will be created first :laughing:

You might try to define availability on your template sensor, checking if the attribute is there.
Normally, if the sensor availability is false, the value_template won’t be evaluated.

1 Like