How to check availability of a state attribute that returns a non-scalar value

Hi all,

After splitting a big configuration file into smaller files using the packages approach, I now get some errors in the log file at startup. It appears that some state attributes of a sensor are not initially available during startup even though the sensor value itself is available. Once Home Assistant has fully started the sensors work as intended. The state attribute which seems to be unavailable is not a scalar, and I cannot figure out how to test if the state attribute is available.

I have tried to reproduce the problem with the following simplified example:
template:

  - sensor:
    - name: "a_debug_sensor_for_yaml"
      unique_id: "a_debug_sensor_for_yaml"
      unit_of_measurement: "DKK/kWh"
      state: >
        {% set this_is_the_debug_sensor = 1 %}
        {% set ns = namespace() %}
        {% set ns.p = state_attr('sensor.Wrapped_Nordpool', 'raw_today_buy') %}
        {% if ns.p not in ['unknown', 'unavailable', 'null', 'none'] %}
          {{ ns.p|length }}
        {% endif %}
        availability: >
          {{ is_number(states('sensor.Wrapped_Nordpool')) }}

Once Home Assistant has started the debug sensor returns:
24

And the following test code…:

{% set this_is_the_debug_sensor = 1 %}
{% set ns = namespace() %}
{% set ns.p = state_attr('sensor.Wrapped_Nordpool', 'raw_today_buy') %}
{% if ns.p not in ['unknown', 'unavailable', 'null', 'none'] %}
  {{ ns.p|length }}
{% endif %}
{{ is_number(states('sensor.Wrapped_Nordpool')) }}
{{state_attr('sensor.Wrapped_Nordpool', 'raw_today_buy')}}

…returns the following in the Template editor:
24

True
[{‘start’: ‘2023-07-02T00:00:00+02:00’, ‘end’: ‘2023-07-02T01:00:00+02:00’, ‘value’: 1.573}, {‘start’: ‘2023-07-02T01:00:00+02:00’, ‘end’: ‘2023-07-02T02:00:00+02:00’, ‘value’: 1.449}, {‘start’: ‘2023-07-02T02:00:00+02:00’, ‘end’: ‘2023-07-02T03:00:00+02:00’, ‘value’: 1.42}, {‘start’: ‘2023-07-02T03:00:00+02:00’, ‘end’: ‘2023-07-02T04:00:00+02:00’, ‘value’: 1.42}, {‘start’: ‘2023-07-02T04:00:00+02:00’, ‘end’: ‘2023-07-02T05:00:00+02:00’, ‘value’: 1.42}, {‘start’: ‘2023-07-02T05:00:00+02:00’, ‘end’: ‘2023-07-02T06:00:00+02:00’, ‘value’: 1.419}, {‘start’: ‘2023-07-02T06:00:00+02:00’, ‘end’: ‘2023-07-02T07:00:00+02:00’, ‘value’: 1.419}, {‘start’: ‘2023-07-02T07:00:00+02:00’, ‘end’: ‘2023-07-02T08:00:00+02:00’, ‘value’: 1.415}, {‘start’: ‘2023-07-02T08:00:00+02:00’, ‘end’: ‘2023-07-02T09:00:00+02:00’, ‘value’: 1.392}, {‘start’: ‘2023-07-02T09:00:00+02:00’, ‘end’: ‘2023-07-02T10:00:00+02:00’, ‘value’: 1.28}, {‘start’: ‘2023-07-02T10:00:00+02:00’, ‘end’: ‘2023-07-02T11:00:00+02:00’, ‘value’: 1.075}, {‘start’: ‘2023-07-02T11:00:00+02:00’, ‘end’: ‘2023-07-02T12:00:00+02:00’, ‘value’: 0.507}, {‘start’: ‘2023-07-02T12:00:00+02:00’, ‘end’: ‘2023-07-02T13:00:00+02:00’, ‘value’: -0.143}, {‘start’: ‘2023-07-02T13:00:00+02:00’, ‘end’: ‘2023-07-02T14:00:00+02:00’, ‘value’: -1.064}, {‘start’: ‘2023-07-02T14:00:00+02:00’, ‘end’: ‘2023-07-02T15:00:00+02:00’, ‘value’: -2.676}, {‘start’: ‘2023-07-02T15:00:00+02:00’, ‘end’: ‘2023-07-02T16:00:00+02:00’, ‘value’: -2.293}, {‘start’: ‘2023-07-02T16:00:00+02:00’, ‘end’: ‘2023-07-02T17:00:00+02:00’, ‘value’: 0.264}, {‘start’: ‘2023-07-02T17:00:00+02:00’, ‘end’: ‘2023-07-02T18:00:00+02:00’, ‘value’: 1.092}, {‘start’: ‘2023-07-02T18:00:00+02:00’, ‘end’: ‘2023-07-02T19:00:00+02:00’, ‘value’: 1.364}, {‘start’: ‘2023-07-02T19:00:00+02:00’, ‘end’: ‘2023-07-02T20:00:00+02:00’, ‘value’: 1.465}, {‘start’: ‘2023-07-02T20:00:00+02:00’, ‘end’: ‘2023-07-02T21:00:00+02:00’, ‘value’: 1.685}, {‘start’: ‘2023-07-02T21:00:00+02:00’, ‘end’: ‘2023-07-02T22:00:00+02:00’, ‘value’: 1.683}, {‘start’: ‘2023-07-02T22:00:00+02:00’, ‘end’: ‘2023-07-02T23:00:00+02:00’, ‘value’: 1.618}, {‘start’: ‘2023-07-02T23:00:00+02:00’, ‘end’: ‘2023-07-03T00:00:00+02:00’, ‘value’: 1.576}]

But in the log file I get the following at startup:
2023-07-02 21:09:29.138 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template<template=({% set this_is_the_debug_sensor = 1 %} {% set ns = namespace() %} {% set ns.p = state_attr(‘sensor.Wrapped_Nordpool’, ‘raw_today_buy’) %} {# if ns.p != ‘None’) and (ns.p != ‘none’) and (ns.p != ‘Unknown’) and (ns.p != ‘unknown’) #} {% if ns.p not in [‘unknown’, ‘unavailable’, ‘null’, ‘none’] %}
{{ ns.p|length }}
{% endif %} availability: >
{{ is_number(states(‘sensor.Wrapped_Nordpool’)) }}) renders=2>
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 563, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 2179, in _render_with_context
return template.render(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/site-packages/jinja2/environment.py”, line 1301, in render
self.environment.handle_exception()
File “/usr/local/lib/python3.11/site-packages/jinja2/environment.py”, line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File “”, line 2, in top-level template code
TypeError: object of type ‘NoneType’ has no len()
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 684, in async_render_to_info
render_info._result = self.async_render(variables, strict=strict, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 565, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: object of type ‘NoneType’ has no len()

If I remove the following line:
{{ ns.p|length }}
I get no error in the log file for the debug sensor.

I cannot figure out how to test if ns.p is available, i.e., if it is defined and populated. I kindly ask for your advice on how to do this.

Thank you in advance!