Can someone here help me understand how to fix my "current_forecast" and "tomorrow_forecast" template weather sensor. Both show unavailable

Hello all,

Can I get some help on fixing my weather forecast sensors? I honestly don’t know where to start. I heard they broke because some kind of change home assistant made within the last week.

Essentially, I inherited the templates etc. from a github repo of someone that is/ was a programmer and now that I’ve had a problem with one breaking. I don’t know how to fix it.

I am ok with changing sources if that has to happen. – Below is the template sensor (one of them)

I tried to fix it myself by changing the integration being used. It used to pull the data from pirateweather. I switched over to “Meteorologisk institutt” and changed the info from what it originally was to “weather.forecast_home” which is what my entity is called for the “Meteorologisk institutt” integration. However, my current forecast entity still says “unavailable”.

Is there something I have to do to “load” the information to the sensor for the first time?

I apologize, I’ve never fixed something like this before and I’m pretty confused.

Another thing that kind of confuses me (2nd portion of the code post below) is that the “current forecast detail” didn’t break- It is still working properly

  - platform: template
    sensors:
      current_forecast:
        friendly_name: Current Forecast
        # #unit_of_measurement: ""
        icon_template: mdi:weather-sunny
        value_template: >-
          {% set forecast = state_attr('weather.forecast_home','forecast')[0] %}
          {{ forecast.condition }}
        attribute_templates:
          high_temp: >-
            {% set forecast = state_attr('weather.forecast_home','forecast')[0] %}
            {{ forecast.temperature }}
          overnight_low: >-
            {% set forecast = state_attr('weather.forecast_home','forecast')[1] %}
            {{ forecast.templow }}
          wind_bearing: >-
            {% set forecast = state_attr('weather.forecast_home','forecast')[0] %}
            {{ forecast.wind_bearing }}
          wind_speed: >-
            {% set forecast = state_attr('weather.forecast_home','forecast')[0] %}
            {{ forecast.wind_speed }}
          precipitation: >-
            {% set forecast = state_attr('weather.forecast_home','forecast')[0] %}
            {{ forecast.precipitation }}
      current_forecast_detail:
        friendly_name: Current Forecast Detail
        # #unit_of_measurement: ""
        icon_template: mdi:weather-sunny
        value_template: >-
          {%- macro getReport() -%}
            {{ [
              'Today in Morristown ', 
              'Later Today ',
              'For the rest of the day'
              ] | random }}
            expect a high of {{ state_attr('sensor.current_forecast','high_temp') }}
            {%- if states('sensor.current_forecast') == 'sunny' and is_state('binary_sensor.night','off')%}
              {{ [
                  'and sunny.', 
                  'with sun.',
                  'with sunny conditions prevailing.'
                  ] | random }}
            {%- elif states('sensor.current_forecast') == 'sunny' and is_state('binary_sensor.night','on') %}
              {{ [
                'and clear.', 
                'with clear skies.'
                ] | random }}
            {%- elif states('sensor.current_forecast') == 'clear-night' %}
              {{ [
                'and clear.', 
                'with clear skies.'
                ] | random }}
            {%- elif states('sensor.current_forecast') == 'rainy' %}
              {{ [
                'with rain.', 
                'with showers.',
                'and rainy.'
                ] | random }}
              {{ [
                'Rainfall accumulations ', 
                'Total Rainfall amounts '
                ] | random }}
                near {{ state_attr('sensor.current_forecast','precipitation') }} inches
                possible.
            {%- elif states('sensor.current_forecast') == 'snowy' %}
              {{ [
                'with snow.', 
                'with snow showers.'
                ] | random }}
              {{ [
                'Snowfall accumulations ', 
                'Total snowfall amounts '
                ] | random }}
                near {{ state_attr('sensor.current_forecast','precipitation') }} inches
                possible.
            {%- elif states('sensor.current_forecast') == 'snowy-rainy' %}
              {{ [
                  'with mix preciptation possible.', 
                  'with snow and rain showers.'
                  ] | random }}
              Total accumulation near {{ state_attr('sensor.current_forecast','precipitation') }} inches
                possible.    
            {%- elif states('sensor.current_forecast') == 'windy' %}
              {{ [
                'and windy.', 
                'with lots of wind.'
                ] | random }}
            {%- elif states('sensor.current_forecast') == 'fog' %}
              {{ [
                'and foggy.', 
                'with some fog.'
                ] | random }}
            {% elif states('sensor.current_forecast') == 'cloudy' %}
              {{ [
                'with clouds.', 
                'with cloudy skies.'
                ] | random }}
            {% elif states('sensor.current_forecast') == 'partlycloudy' %}
              {{ [
                  'with some clouds.', 
                  'with partly cloudy skies.',
                  'with scattered clouds'
                  ] | random }}
            {%- elif states('sensor.current_forecast') == 'hail' %}
              {{ [
                'with severve thunderstorms possible.', 
                'with hail possible.'
                ] | random }}
              {{ [
                'Rainfall accumulations ', 
                'Total Rainfall amounts '
                ] | random }}
                near {{ state_attr('sensor.current_forecast','precipitation') }} inches
                possible.
            {%- elif states('sensor.current_forecast') == 'lightning' %}
              {{ [
                'with thunderstorms possible.', 
                'with the potential of thunderstorms.'
                ] | random }}
              {{ [
                'Rainfall accumulations ', 
                'Total Rainfall amounts '
                ] | random }}
                near {{ state_attr('sensor.current_forecast','precipitation') }} inches
                possible.
            {% endif -%}

            {{ [
                'Later tonight expect a low of ', 
                'Overnight expect a low of '
                ] | random }}
            {{ state_attr('sensor.current_forecast','overnight_low') }} degrees. 
          {%- endmacro -%}
          {# a macro that removes all newline characters, empty spaces, and returns formatted text  #}
            {%- macro cleanup(data) -%}
              {%- for item in data.split("\n")  if item | trim != "" -%}
                {{ item | trim }} {% endfor -%}
          {%- endmacro -%}

          {# a macro to call all macros :)  #}
            {%- macro mother_of_all_macros() -%}
              {{ getReport() }}
            {%- endmacro -%}
            
            {# Call the macro  #}
            {{- cleanup(mother_of_all_macros()) -}}

Thank you for this!!

1 Like

For future reference, the Weather integration’s documentation contains an example of a Trigger-based Template Sensor that gets and reports weather forecast data.

1 Like