Handle None values

Hi all,

I need help with ’ None ’ value, Im trying getting mdi icon for use.

      icon: |
        {% set forecast = state_attr('weather.openweathermap', 'forecast') %}
        {% set precipitation_type = states('sensor.openweathermap_precipitation_kind') | default('None', true) %}
        {% if precipitation_type == 'None' %}
          mdi:weather-{{ forecast[0].condition }}
        {% elif precipitation_type == 'Snow' or (forecast and forecast[0] and forecast[0].condition == 'snowy') %}
          mdi:weather-snowy-heavy
        {% elif forecast and forecast[0] and forecast[0].condition == 'snowy-rainy' %}
          mdi:weather-snowy-rainy
        {% elif precipitation_type == 'Rain' or forecast and forecast[0] and forecast[0].condition == 'rainy' %}
          mdi:weather-rainy
        {% elif forecast and forecast[0] and forecast[0].condition == 'partlycloudy' %}
          mdi:weather-partly-cloudy
        {% else %}
          mdi:weather-{{ forecast[0].condition }}
        {% endif %}

im getting this error;

Template variable error: None has no element 0 when rendering '{% set forecast = state_attr('weather.openweathermap', 'forecast') %} {% set precipitation_type = states('sensor.openweathermap_precipitation_kind') | default('None', true) %} {% if precipitation_type == 'None' %} mdi:weather-{{ forecast[0].condition }} {% elif precipitation_type == 'Snow' or (forecast and forecast[0] and forecast[0].condition == 'snowy') %} mdi:weather-snowy-heavy {% elif forecast and forecast[0] and forecast[0].condition == 'snowy-rainy' %} mdi:weather-snowy-rainy {% elif precipitation_type == 'Rain' or forecast and forecast[0] and forecast[0].condition == 'rainy' %} mdi:weather-rainy {% elif forecast and forecast[0] and forecast[0].condition == 'partlycloudy' %} mdi:weather-partly-cloudy {% else %} mdi:weather-{{ forecast[0].condition }} {% endif %}'

that’s my full binary_sensor code:

- binary_sensor:
    - name: notification_on_gunluk_yagis
      unique_id: notification_on_gunluk_yagis
      icon: |
        {% set forecast = state_attr('weather.openweathermap', 'forecast') %}
        {% set precipitation_type = states('sensor.openweathermap_precipitation_kind') | default('None', true) %}
        {% if precipitation_type == 'None' %}
          mdi:weather-{{ forecast[0].condition }}
        {% elif precipitation_type == 'Snow' or (forecast and forecast[0] and forecast[0].condition == 'snowy') %}
          mdi:weather-snowy-heavy
        {% elif forecast and forecast[0] and forecast[0].condition == 'snowy-rainy' %}
          mdi:weather-snowy-rainy
        {% elif precipitation_type == 'Rain' or forecast and forecast[0] and forecast[0].condition == 'rainy' %}
          mdi:weather-rainy
        {% elif forecast and forecast[0] and forecast[0].condition == 'partlycloudy' %}
          mdi:weather-partly-cloudy
        {% else %}
          mdi:weather-{{ forecast[0].condition }}
        {% endif %}
      state: |
        {% set precipitation = states('sensor.openweathermap_forecast_precipitation' ) %}
        {% if precipitation | float(0) > 0.0 %}
          on
        {% else %}
          off
        {% endif %}
      attributes:
        icon_color: "rgb(var(--color-blue))"
        text_summary: |
          {% set forecast = state_attr('weather.openweathermap', 'forecast') %}
          {% set precipitation = states('sensor.openweathermap_forecast_precipitation') %}
          {% set precipitation_type = states('sensor.openweathermap_precipitation_kind') | default('None', true) %}
          {% if precipitation | float(0) > 0.0 %}
            {% if precipitation_type == 'Snow' or (forecast and forecast[0] and forecast[0].condition == 'snowy') %}
              Kar yağışı bekleniyor {{ precipitation }}mm.
            {% elif forecast and forecast[0] and forecast[0].condition == 'snowy-rainy' %}
              Sulu kar bekleniyor {{ precipitation }}mm.
            {% elif precipitation_type == 'None' %}
              Yağmur
            {% else %}
              Yağmur bekleniyor {{ precipitation }}mm.
            {% endif %}
          {% else %}
            Yağış beklenmiyor.
          {% endif %}
        text_description: |
          {% set forecast = state_attr('weather.openweathermap', 'forecast') %}
          {% if forecast and forecast[0] and forecast[0].precipitation %}
            Tahmini {{ forecast[0].precipitation }}mm. Olasılık %{{ forecast[0].precipitation_probability }}
          {% else %}
            Yağış beklenmiyor
          {% endif %}
        tap_action: more-info
        tap_action_entity: weather.openweathermap