Occasional template error: object of type 'NoneType' has no len()

This one’s been nagging at me for a while and I’m just not seeing the fix - I’d appreciate another opinion at this point. I never see the state of the source sensor when this error occurs because it’s typically logged overnight.
The same error is logged for each sensor and always follows an update error:
Error fetching openweathermap data: HTTPSConnectionPool(host='api.openweathermap.org', port=443)
TypeError: object of type 'NoneType' has no len()') while processing template

I get a periodic error in my logs (presumably when my weather.openweather fails to update) that I’m looking to handle properly in these two template sensors.

The sensors are designed to indicate imminent snow with a simple true / false for use in some commute related automations…

    snow_next_3hrs:
       friendly_name: "Snow in the next 3 hours"
       value_template: >
         {% if (states('weather.openweathermap') not in ['Unavailable', 'None', 'Unknown']) and (states('weather.openweathermap') and ((state_attr('weather.openweathermap', 'forecast') | count) > 0)) %}
               {{ (state_attr('weather.openweathermap', 'forecast')[0].condition | regex_match(find='snow', ignorecase=False) and state_attr('weather.openweathermap', 'forecast')[0].precipitation | float >= 30)
                   or (states('sensor.bedford_probability_of_precipitation') | float >= 70.0 and states('sensor.bedford_weather') | regex_match(find='snow', ignorecase=False)) }}
         {% else %}
                {{ false }}
         {% endif %}
       availability_template: >
         {{ (states('weather.openweathermap') not in ['Unavailable', 'None', 'Unknown'])
             and (states('sensor.bedford_weather') not in ['Unavailable', 'None', 'Unknown']) and (states('sensor.bedford_weather') ['Unavailable', 'None', 'Unknown']) }}
    snow_next_12hrs:
       friendly_name: "Snow in the next 12 hours"
       value_template: >
         {% if (states('weather.openweathermap') not in ['Unavailable', 'None', 'Unknown']) and (states('weather.openweathermap') and ((state_attr('weather.openweathermap', 'forecast') | count) > 3)) %}
            {{(state_attr('weather.openweathermap', 'forecast')[0].condition | regex_match(find='snow', ignorecase=False) and state_attr('weather.openweathermap', 'forecast')[0].precipitation | float >= 30)
              or (state_attr('weather.openweathermap', 'forecast')[1].condition | regex_match(find='snow', ignorecase=False) and state_attr('weather.openweathermap', 'forecast')[1].precipitation | float >= 30)
              or (state_attr('weather.openweathermap', 'forecast')[2].condition | regex_match(find='snow', ignorecase=False) and state_attr('weather.openweathermap', 'forecast')[2].precipitation | float >= 30)
              or (state_attr('weather.openweathermap', 'forecast')[3].condition | regex_match(find='snow', ignorecase=False) and state_attr('weather.openweathermap', 'forecast')[3].precipitation | float >= 30)}}
         {% else %}
                {{ false }}
         {% endif %}
       availability_template: "{{ (states('weather.openweathermap') not in ['Unavailable', 'None', 'Unknown']) }}" 

Try this instead:

not in ['unavailable', 'none', 'unknown']

as these states are lower case despite what is confusingly shown in Lovelace.

2 Likes

Thanks - I’ve adjusted and will see what tonight brings.

Yea, never use the states shown in the interface. I always go to the developer/states panel to figure out what I’m looking for.