Weather.get_forecasts returning "Forecast"

I have been using the following template sensor for almost one year, now it is returning a value of “Forecast” instead of the numerical forecast temperature. What changed with Home Assistant?

  - trigger:
      - platform: state
        entity_id: weather.home
      - platform: homeassistant
        event: start
    action:
      - action: weather.get_forecasts
        target:
          entity_id: weather.home
        data:
          type: daily
        response_variable: daily
    sensor:
      - name: Today's Forecast Temperature
        unique_id: temperature_forecast_today
        state: "Forecast"
        attributes: 
          temperature: "{{ daily['weather.home'].forecast[0].temperature }}"

Nothing changed in Home Assistant…

You have literally set your state to “Forecast”:

image

If you want the template to supply the value for state, then the template needs to be where “Forecast” is.

When I do this

    sensor:
      - name: Today's Forecast Temperature
        unique_id: temperature_forecast_today
        state: "{{ daily['weather.home'].forecast[0].temperature }}"
        unit_of_measurement: "°F"

I get the following error:

Error adding entity sensor.today_s_forecast_temperature for domain sensor with platform template
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 640, in state
    numerical_value = float(value)  # type:ignore[arg-type]
ValueError: could not convert string to float: 'None'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 609, in _async_add_entities
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 928, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1384, in add_to_platform_finish
    self.async_write_ha_state()
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1023, in async_write_ha_state
    self._async_write_ha_state()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1148, in _async_write_ha_state
    self.__async_calculate_state()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1085, in __async_calculate_state
    state = self._stringify_state(available)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1029, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 642, in state
    raise ValueError(
    ...<5 lines>...
    ) from err
ValueError: Sensor sensor.today_s_forecast_temperature has device class 'None', state class 'None' unit '°F' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'None' (<class 'str'>)

Getting closer…

When I remove unit_of_measurement: "°F" it loads without an error, but the value returned is “None”

  - trigger:
      - platform: state
        entity_id: weather.home
      - platform: homeassistant
        event: start
    action:
      - action: weather.get_forecasts
        target:
          entity_id: weather.home
        data:
          type: daily
        response_variable: daily
    sensor:
      - name: Today's Forecast Temperature
        unique_id: temperature_forecast_today
        state: "{{ daily['weather.home'].forecast[0].temperature }}"

Sensors with unit_of_measurement defined require that the state be a numerical value.

If you want a unit you need to add an availability to avoid issues at startup when the weather entity hasn’t fully loaded. It also wouldn’t hurt to reject certain state changes of weather.home.

  - trigger:
      - platform: state
        entity_id: weather.home
        not_to:
          - unknown
          - unavailable
      - platform: homeassistant
        event: start
    action:
      - action: weather.get_forecasts
        target:
          entity_id: weather.home
        data:
          type: daily
        response_variable: daily
    sensor:
      - name: Today's Forecast Temperature
        unique_id: temperature_forecast_today
        state: "{{ daily['weather.home'].forecast[0].temperature }}"
        unit_of_measurement: "°F"
        device_class: temperature
        availability: "{{ has_value('weather.home') }}"
1 Like