My weather forecast template doesnt work anymore

i not sure what the corrected version is… and this is what i currently have now

template:
  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.peterborough_forecast
        response_variable: hourly
      - variables:
          ptb_forecast: "{{ hourly['weather.peterborough_forecast'].forecast }}"
    sensor:
      - name: Temperature forecast next hour2
        unique_id: temperature_forecast_next_hour
        state: "{{ ptb_forecast[0].temperature }}"
        unit_of_measurement: °C

  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - service: weather.get_forecasts
        data:
          type: minute
          target:
            entity_id: weather.peterborough_forecast
          response_variable: hourly
    sensor:
      - name: Temperature forecast next hour
        unique_id: temperature_forecast_next_hour
        state: "{{ hourly['weather.peterborough_forecast'].forecast[0].temperature }}"
        unit_of_measurement: °C

  - sensor:
      - name: Today's mimimal temperature
        unique_id: today_min
        #states: "{{ states('sensor.peterborough_low_temperature' ) }}"
        value_template: "{{ states('sensor.peterborough_low_temperature' ) }}"

      - name: Today's maximal temperature
        unique_id: today_max
        value_template: "{{ states('sensor.peterborough_high_temperature' ) }}"

      - name: Today's rain forecast
        unique_id: today_rain
        value_template: "{{ states('sensor.peterborough_chance_of_precipitation' ) }}"

      - name: "Today weather forecast"
        state: >
          {% if is_state("sensor.peterborough_current_condition",'clear-night') %}
            3
          {% elif is_state("sensor.peterborough_current_condition",'cloudy') %}
            5
          {% elif is_state("sensor.peterborough_current_condition",'fog') %}
            16
          {% elif is_state("sensor.peterborough_current_condition",'hail') %}
            6
          {% elif is_state("sensor.peterborough_current_condition",'lightning') %}
            13
          {% elif is_state("sensor.peterborough_current_condition",'lightning-rainy') %}
            17
          {% elif is_state("sensor.peterborough_current_condition",'partlycloudy') %}
            8
          {% elif is_state("sensor.peterborough_current_condition",'pouring') %}
            18
          {% elif is_state("sensor.peterborough_current_condition",'rainy') %}
            9
          {% elif is_state("sensor.peterborough_current_condition",'snowy') %}
            11
          {% elif is_state("sensor.peterborough_current_condition",'snowy-rainy') %}
            7
          {% elif is_state("sensor.peterborough_current_condition",'windy') %}
            14
          {% elif is_state("sensor.peterborough_current_condition",'windy-variant') %}
            14
          {% else %}
            12
          {% endif %}

so what is the corrected version i need like the exact sample

# This is Home Assistant configuration. This goes into your configuration.yaml file (or packages folder if you have split your config)
# you need to provide your API key

template:
  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.peterborough_forecast
        response_variable: hourly
      - variables:
          ptb_forecast: "{{ hourly['weather.peterborough_forecast'].forecast }}"
    sensor:
      - name: Temperature forecast next hour
        unique_id: temperature_forecast_next_hour
        state: "{{ ptb_forecast[0].temperature }}"
        unit_of_measurement: °C      
        attributes:
          forecast_0: "{{ ptb_forecast[0] }}"

  - sensor:
      - name: Today Min
        unique_id: todays_minimal_temperature
        state: "{{ states('sensor.peterborough_low_temperature' ) }}"
        availability: "{{ has_value('sensor.peterborough_low_temperature') }}"
        
      - name: Today Max
        unique_id: todays_maximal_temperature
        state: "{{ states('sensor.peterborough_high_temperature' ) }}"
        availability: "{{ has_value('sensor.peterborough_high_temperature') }}"

      - name: Today Rain
        unique_id: todays_rain_forecast
        state: "{{ states('sensor.peterborough_chance_of_precipitation') }}"
        availability: "{{ has_value('sensor.peterborough_chance_of_precipitation') }}"   
   
      - name: Today Icon
        unique_id: today_condition_icon
        state: >-
          {% set condition = states("weather.peterborough_forecast") %}
          {% set mapper = {'clear-night': 3,
          'cloudy': 5, 'fog': 16, 'hail': 6,
          'lightning': 13, 'lightning-rainy': 17, 'partlycloudy': 8,
          'pouring': 18, 'rainy': 9, 'snowy': 11,
          'snowy-rainy': 7, 'windy': 14, 'windy-variant': 14 } %}
          {{ mapper[condition] if condition in mapper.keys() else 12 }}
        availability: "{{ has_value('weather.peterborough_forecast') }}"

EDIT: Typo corrected, missing comma and quote marks

ok ill try that

i never found anything like that … and i appreciate the help so far

still not working all 4 sensors come up as unavailable even though the sensors they supposed to read from 3 of them have a value…

something else i should try? i checked the logs i didnt see no error there …




The only issue I can see off-hand is it that the sensor for weather condition, sensor.peterborough_current_condition, reports it’s state with the “pretty” format “Mostly Cloudy” instead of one of the normal condition names like “partlycloudy”. This shouldn’t cause the issue you’re inquiring about, but it does mean that the template needs to be adjusted. The keys in the dictionary, mapper, need to match the value returned by the entity used in condition.

Copy the configurations for all the state-based sensors, paste it into the Template tool in Developer Tools, then let us know what it returns.

i guess thats why i have that {else} in the code i edited twice if it doesnt come up with any of those words then icon will be 12

glitch with website my post dis appearted… but i was saying the code i had and edited twice… has an else so if non of them words show then the icon will be number 12

and is there a way its always annoyed me… the weather updates i think like every 10 min but with my poor internet it always goes unaviable… anything to fix that?

so i did a test… i changed things around the names…cuz i needed them to match the names orginally i had for sensor like sensor.today_max … so what i did was i deleted the 4 sensors in the Devices section…

i renamed you see below… and i rebooted fully… when i check the states… it doesnt actually create the sensors…

  - sensor:
      - name: today_min
        friendly_name: Today's mimimal temperature
        unique_id: today_min
        states: "{{ states('sensor.peterborough_low_temperature' ) }}"
        availability: "{{ has_value('sensor.peterborough_low_temperature') }}"

      - name: today_max
        friendly_name: Today's maximal temperature
        unique_id: today_max
        state: "{{ states('sensor.peterborough_high_temperature' ) }}"
        availability: "{{ has_value('sensor.peterborough_high_temperature') }}"

      - name: today_rain
        friendly_name: Today's rain forecast
        unique_id: today_rain
        state: "{{ states('sensor.peterborough_chance_of_precipitation') }}"
        availability: "{{ has_value('sensor.peterborough_chance_of_precipitation') }}"

      - name: today_icon
        friendly_name: Today weather forecast
        unique_id: today_icon
        state: >-
          {% set condition = states("sensor.peterborough_current_condition") %}
          {% set mapper = {'clear-night': 3,
          'cloudy': 5, 'fog': 16, 'hail': 6,
          'lightning': 13, 'lightning-rainy': 17, 'partlycloudy': 8,
          'pouring': 18, 'rainy': 9, 'snowy': 11,
          'snowy-rainy': 7, 'windy': 14, 'windy-variant': 14 } %}
          {{ mapper[condition] if condition in mapper.keys() else 12 }}
        availability: "{{ has_value('weather.peterborough_forecast') }}"

so maybe its not right to create the sensor?

friendly_name is not a valid configuration variable in the current format.

If you want the name and entity ID to be different you can change them in the UI.

ah ok… i took out the friendly name… and reboot HA but it didnt create the sensors still all i did above just ## pound them out… and restarted… so still wont make those sensors
must be something just 1 small thing

You need to just delete everything… I think you’ve got stuff that is not properly commented out and it’s causing the file to not be loaded properly or you have unique IDs that aren’t unique…

I added the Environment Canada integration to my instance, pasted the sensor config from my post above, and this is the result:

This is the exact configuration I used for the pictured results
template:
  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.peterborough_forecast
        response_variable: hourly
      - variables:
          ptb_forecast: "{{ hourly['weather.peterborough_forecast'].forecast }}"
    sensor:
      - name: Temperature forecast next hour
        unique_id: temperature_forecast_next_hour
        state: "{{ ptb_forecast[0].temperature }}"
        unit_of_measurement: °C      
        attributes:
          forecast_0: "{{ ptb_forecast[0] }}"

  - sensor:
      - name: Today Min
        unique_id: todays_minimal_temperature_0001
        state: "{{ states('sensor.peterborough_low_temperature' ) }}"
        availability: "{{ has_value('sensor.peterborough_low_temperature') }}"
        
      - name: Today Max
        unique_id: todays_maximal_temperature_0001
        state: "{{ states('sensor.peterborough_high_temperature' ) }}"
        availability: "{{ has_value('sensor.peterborough_high_temperature') }}"

      - name: Today Rain
        unique_id: todays_rain_forecast_0001
        state: "{{ states('sensor.peterborough_chance_of_precipitation') }}"
        availability: "{{ has_value('sensor.peterborough_chance_of_precipitation') }}"   
  
      - name: Today Icon
        unique_id: today_condition_icon_0001
        state: >-
          {% set condition = states("weather.peterborough_forecast") %}
          {% set mapper = {'clear-night': 3,
          'cloudy': 5, 'fog': 16, 'hail': 6,
          'lightning': 13, 'lightning-rainy': 17, 'partlycloudy': 8,
          'pouring': 18, 'rainy': 9, 'snowy': 11,
          'snowy-rainy': 7, 'windy': 14, 'windy-variant': 14 } %}
          {{ mapper[condition] if condition in mapper.keys() else 12 }}
        availability: "{{ has_value('weather.peterborough_forecast') }}"

FWIW, in your post in the other thread you are using value_template which is not a valid configuration variable in the current format.

its finally working yaaaaa… what i ended up doing… was i removed the weather.yaml and rebooted HA and then i checked states… and i seen the sensors were still there and the temp ones from i was making… so i i went to settings devices and entities… and searched for them. and deleted them… and did another reboot… check for sensors just to see if anything… conflicting… and then i put the weather.yaml back… and rebooted… and this time it worked… i guessing something was stuck and wouldnt fix itself… so seems to be working…
what does FWIW mean?
and ya i put back state: as value:template i trying something… i did find if i put it under the weather trigger automation under the sensor… it wouldnt work for me… it but id have to keep it under the - sensor:

another question… i tried

        ### Today's rain forecast ###
      - name: today_rain
        state: "{{ states('sensor.peterborough_chance_of_precipitation') | Default(0)}}"
        availability: "{{ has_value('sensor.peterborough_chance_of_precipitation')}}"

but since thats wrong what is the right code when its unavalable… the value should be 0

For what it’s worth

If you want it to report 0 even if the source sensor is unavailable or unknown then you can remove the availability. Also, Default() is not a valid filter function. Case matters; the filter is default().

        ### Today's rain forecast ###
      - name: today_rain
        state: "{{ states('sensor.peterborough_chance_of_precipitation') | int(0) }}"

If you want to be able to edit the entities’ names or IDs in the UI you will need to give them unique IDs.

ok cool ill try those and learn something new everyday FWIW (:

so i removed the default… and i removed the avaliablity but it still made the sensor unknown… or do i need that default in there?

Sorry about that… default() is for undefined values, but when the source sensor returns “unknown” or “unavailable” those values are considered defined. You will need to use a different method. Since this is for a numerical value I would suggest int(0) or float(0)

### Today's rain forecast ###
      - name: today_rain
        state: "{{ states('sensor.peterborough_chance_of_precipitation') | int(0) }}"

ok that works. thanks learn something new everyday… glad you know all this code… you must deal with it every day…

i know i still wanna do i asked a year or 2 ago but i still wanna do it … i wanna make a sensor with multiple attributes. like the weather sensor… but no one ever got back to me… i wanted it it for a mqtt and also for a couple things like a message to a display screen… so message had title font color message but no one ever knew… do you know how that works… not something i wanna work on today… but if you know could point me in the right direction for that

I don’t use MQTT very much, but it’s going to depend on how the MQTT topic and payload are set up and/or where the different data points are coming from. If the values for the sensor’s state and all the attributes all come from MQTT, then use an MQTT sensor otherwise you may need to create multiple sensors.

The devil is in the details.

I don’t understand what you are asking about.