Storing only subset of forecast data in a template sensor

Hi,

I try to reduce the forecast data received from a service, since I am getting a warning message in HA, that the data exceeds a limit. Anyways, it is probably a good idea to only store the information that I am interested in.
Here is how my template sensor looked in the first place, getting alle forecast data provided:

# Astroweather
- trigger:
    - platform: time_pattern
      hours: /1
    - platform: homeassistant
      event: start
  action:
    - service: weather.get_forecasts
      target:
        entity_id:
          - weather.astroweathe
      data:
        type: hourly
      response_variable: hourly_forecast
  sensor:
    - name: Astroweather hourly Forecast
      unique_id: astroweather_hourly_forecast
      state: "{{ now().isoformat() }}"
      icon: mdi:hours-24
      attributes:
        forecast: "{{ hourly_forecast['weather.astroweather']['forecast'][:36] }}"

I changed it to the following template:

# Astroweather
- trigger:
    - platform: time_pattern
      hours: /1
    - platform: homeassistant
      event: start
  action:
    - service: weather.get_forecasts
      target:
        entity_id:
          - weather.astroweather_dresden
      data:
        type: hourly
      response_variable: hourly_forecast
  sensor:
    - name: Astroweather Dresden hourly Forecast
      unique_id: astroweather_dresden_hourly_forecast
      state: "{{ now().isoformat() }}"
      icon: mdi:hours-24
      attributes:
        forecast: |-
          {%- set data = namespace(forecast_list=[]) -%}
          {%- for entry in hourly_forecast['weather.astroweather_dresden']['forecast'] -%}
            {%- set data.forecast_list = data.forecast_list + [dict(datetime=entry.datetime, cloudless_percentage=entry.cloudless_percentage, condition=entry.condition, lifted_index=entry.lifted_index, seeing_percentage=entry.seeing_percentage, temperature=entry.temperature, transparency_percentage=entry.transparency_percentage, humidity=entry.humidity)] -%}
          {%- endfor -%}
          {{ data.forecast_list }}

Unfortunately the forecast attribute from my template sensor is now very messy. The list and the dictionary does not get parsed. How can I fix this?

forecast: [{'datetime': datetime.datetime(2024, 6, 19, 12, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 0, 'lifted_index': 2, 'seeing_percentage': 75, 'temperature': 21.3, 'transparency_percentage': 22, 'humidity': 70}, {'datetime': datetime.datetime(2024, 6, 19, 13, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 0, 'lifted_index': 2, 'seeing_percentage': 75, 'temperature': 20.3, 'transparency_percentage': 22, 'humidity': 72}, {'datetime': datetime.datetime(2024, 6, 19, 14, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 0, 'lifted_index': 2, 'seeing_percentage': 75, 'temperature': 19.4, 'transparency_percentage': 22, 'humidity': 76}, {'datetime': datetime.datetime(2024, 6, 19, 15, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 7, 'lifted_index': 6, 'seeing_percentage': 75, 'temperature': 18.1, 'transparency_percentage': 0, 'humidity': 79}, {'datetime': datetime.datetime(2024, 6, 19, 16, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 18, 'lifted_index': 6, 'seeing_percentage': 75, 'temperature': 17.2, 'transparency_percentage': 0, 'humidity': 83}, {'datetime': datetime.datetime(2024, 6, 19, 17, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 38, 'lifted_index': 6, 'seeing_percentage': 75, 'temperature': 16.7, 'transparency_percentage': 0, 'humidity': 86}, {'datetime': datetime.datetime(2024, 6, 19, 18, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 45, 'condition': 62, 'lifted_index': 6, 'seeing_percentage': 75, 'temperature': 16.2, 'transparency_percentage': 0, 'humidity': 89}, {'datetime': datetime.datetime(2024, 6, 19, 19, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 86, 'condition': 72, 'lifted_index': 6, 'seeing_percentage': 75, 'temperature': 15.5, 'transparency_percentage': 0, 'humidity': 93}, {'datetime': datetime.datetime(2024, 6, 19, 20, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 90, 'condition': 74, 'lifted_index': 6, 'seeing_percentage': 75, 'temperature': 14.2, 'transparency_percentage': 0, 'humidity': 97}, {'datetime': datetime.datetime(2024, 6, 19, 21, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 86, 'condition': 68, 'lifted_index': 10, 'seeing_percentage': 55, 'temperature': 13.3, 'transparency_percentage': 0, 'humidity': 99}, {'datetime': datetime.datetime(2024, 6, 19, 22, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 82, 'condition': 66, 'lifted_index': 10, 'seeing_percentage': 55, 'temperature': 12.2, 'transparency_percentage': 0, 'humidity': 100}, {'datetime': datetime.datetime(2024, 6, 19, 23, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 11, 'condition': 40, 'lifted_index': 10, 'seeing_percentage': 55, 'temperature': 12.1, 'transparency_percentage': 0, 'humidity': 100}, {'datetime': datetime.datetime(2024, 6, 20, 0, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 34, 'lifted_index': 15, 'seeing_percentage': 45, 'temperature': 12.2, 'transparency_percentage': 0, 'humidity': 100}, {'datetime': datetime.datetime(2024, 6, 20, 1, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 34, 'lifted_index': 15, 'seeing_percentage': 45, 'temperature': 12.5, 'transparency_percentage': 0, 'humidity': 100}, {'datetime': datetime.datetime(2024, 6, 20, 2, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 33, 'lifted_index': 15, 'seeing_percentage': 45, 'temperature': 12.7, 'transparency_percentage': 0, 'humidity': 100}, {'datetime': datetime.datetime(2024, 6, 20, 3, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 34, 'lifted_index': 15, 'seeing_percentage': 45, 'temperature': 12.6, 'transparency_percentage': 0, 'humidity': 100}, {'datetime': datetime.datetime(2024, 6, 20, 4, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 35, 'lifted_index': 15, 'seeing_percentage': 45, 'temperature': 12.7, 'transparency_percentage': 0, 'humidity': 99}, {'datetime': datetime.datetime(2024, 6, 20, 5, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 35, 'lifted_index': 15, 'seeing_percentage': 45, 'temperature': 13.2, 'transparency_percentage': 0, 'humidity': 98}, {'datetime': datetime.datetime(2024, 6, 20, 6, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 1, 'condition': 40, 'lifted_index': 15, 'seeing_percentage': 65, 'temperature': 14.6, 'transparency_percentage': 0, 'humidity': 91}, {'datetime': datetime.datetime(2024, 6, 20, 7, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 38, 'lifted_index': 15, 'seeing_percentage': 65, 'temperature': 16.4, 'transparency_percentage': 0, 'humidity': 78}, {'datetime': datetime.datetime(2024, 6, 20, 8, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 4, 'condition': 39, 'lifted_index': 15, 'seeing_percentage': 65, 'temperature': 18.3, 'transparency_percentage': 0, 'humidity': 69}, {'datetime': datetime.datetime(2024, 6, 20, 9, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 1, 'condition': 47, 'lifted_index': 15, 'seeing_percentage': 75, 'temperature': 19.9, 'transparency_percentage': 55, 'humidity': 61}, {'datetime': datetime.datetime(2024, 6, 20, 10, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 11, 'condition': 51, 'lifted_index': 15, 'seeing_percentage': 75, 'temperature': 21.4, 'transparency_percentage': 55, 'humidity': 53}, {'datetime': datetime.datetime(2024, 6, 20, 11, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 6, 'condition': 49, 'lifted_index': 15, 'seeing_percentage': 75, 'temperature': 22.6, 'transparency_percentage': 55, 'humidity': 49}, {'datetime': datetime.datetime(2024, 6, 20, 12, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 48, 'lifted_index': 10, 'seeing_percentage': 75, 'temperature': 23.6, 'transparency_percentage': 65, 'humidity': 46}, {'datetime': datetime.datetime(2024, 6, 20, 13, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 47, 'lifted_index': 10, 'seeing_percentage': 75, 'temperature': 23.8, 'transparency_percentage': 65, 'humidity': 45}, {'datetime': datetime.datetime(2024, 6, 20, 14, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 48, 'lifted_index': 10, 'seeing_percentage': 75, 'temperature': 24.3, 'transparency_percentage': 65, 'humidity': 45}, {'datetime': datetime.datetime(2024, 6, 20, 15, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 48, 'lifted_index': 6, 'seeing_percentage': 75, 'temperature': 23.8, 'transparency_percentage': 65, 'humidity': 49}, {'datetime': datetime.datetime(2024, 6, 20, 16, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 47, 'lifted_index': 6, 'seeing_percentage': 75, 'temperature': 23.8, 'transparency_percentage': 65, 'humidity': 51}, {'datetime': datetime.datetime(2024, 6, 20, 17, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 48, 'lifted_index': 6, 'seeing_percentage': 75, 'temperature': 23.5, 'transparency_percentage': 65, 'humidity': 52}, {'datetime': datetime.datetime(2024, 6, 20, 18, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 43, 'lifted_index': 6, 'seeing_percentage': 55, 'temperature': 22.3, 'transparency_percentage': 55, 'humidity': 59}, {'datetime': datetime.datetime(2024, 6, 20, 19, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 42, 'lifted_index': 6, 'seeing_percentage': 55, 'temperature': 21.6, 'transparency_percentage': 55, 'humidity': 59}, {'datetime': datetime.datetime(2024, 6, 20, 20, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 42, 'lifted_index': 6, 'seeing_percentage': 55, 'temperature': 20.6, 'transparency_percentage': 55, 'humidity': 62}, {'datetime': datetime.datetime(2024, 6, 20, 21, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 4, 'condition': 40, 'lifted_index': 10, 'seeing_percentage': 45, 'temperature': 20.6, 'transparency_percentage': 55, 'humidity': 59}, {'datetime': datetime.datetime(2024, 6, 20, 22, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 27, 'condition': 56, 'lifted_index': 10, 'seeing_percentage': 45, 'temperature': 19.4, 'transparency_percentage': 55, 'humidity': 62}, {'datetime': datetime.datetime(2024, 6, 20, 23, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 30, 'condition': 50, 'lifted_index': 10, 'seeing_percentage': 45, 'temperature': 19.7, 'transparency_percentage': 55, 'humidity': 60}, {'datetime': datetime.datetime(2024, 6, 21, 0, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 22, 'condition': 57, 'lifted_index': 10, 'seeing_percentage': 55, 'temperature': 18.7, 'transparency_percentage': 65, 'humidity': 67}, {'datetime': datetime.datetime(2024, 6, 21, 1, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 38, 'condition': 55, 'lifted_index': 10, 'seeing_percentage': 55, 'temperature': 18.8, 'transparency_percentage': 65, 'humidity': 69}, {'datetime': datetime.datetime(2024, 6, 21, 2, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 41, 'lifted_index': 10, 'seeing_percentage': 55, 'temperature': 18.1, 'transparency_percentage': 65, 'humidity': 75}, {'datetime': datetime.datetime(2024, 6, 21, 3, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 2, 'condition': 37, 'lifted_index': 10, 'seeing_percentage': 45, 'temperature': 17.7, 'transparency_percentage': 55, 'humidity': 79}, {'datetime': datetime.datetime(2024, 6, 21, 4, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 50, 'condition': 55, 'lifted_index': 10, 'seeing_percentage': 45, 'temperature': 18.1, 'transparency_percentage': 55, 'humidity': 80}, {'datetime': datetime.datetime(2024, 6, 21, 5, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 17, 'condition': 44, 'lifted_index': 10, 'seeing_percentage': 45, 'temperature': 19.6, 'transparency_percentage': 55, 'humidity': 78}, {'datetime': datetime.datetime(2024, 6, 21, 6, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 44, 'lifted_index': 6, 'seeing_percentage': 75, 'temperature': 21.6, 'transparency_percentage': 55, 'humidity': 71}, {'datetime': datetime.datetime(2024, 6, 21, 7, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 45, 'lifted_index': 6, 'seeing_percentage': 75, 'temperature': 23.4, 'transparency_percentage': 55, 'humidity': 67}, {'datetime': datetime.datetime(2024, 6, 21, 8, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 7, 'condition': 49, 'lifted_index': 6, 'seeing_percentage': 75, 'temperature': 25.2, 'transparency_percentage': 55, 'humidity': 64}, {'datetime': datetime.datetime(2024, 6, 21, 9, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 12, 'condition': 52, 'lifted_index': 2, 'seeing_percentage': 75, 'temperature': 26.7, 'transparency_percentage': 55, 'humidity': 60}, {'datetime': datetime.datetime(2024, 6, 21, 10, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 48, 'lifted_index': 2, 'seeing_percentage': 75, 'temperature': 28.0, 'transparency_percentage': 55, 'humidity': 57}, {'datetime': datetime.datetime(2024, 6, 21, 11, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 11, 'condition': 54, 'lifted_index': 2, 'seeing_percentage': 75, 'temperature': 28.9, 'transparency_percentage': 55, 'humidity': 55}, {'datetime': datetime.datetime(2024, 6, 21, 12, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 9, 'condition': 0, 'lifted_index': -1, 'seeing_percentage': 75, 'temperature': 29.2, 'transparency_percentage': 55, 'humidity': 54}, {'datetime': datetime.datetime(2024, 6, 21, 13, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 27, 'lifted_index': -1, 'seeing_percentage': 75, 'temperature': 28.2, 'transparency_percentage': 55, 'humidity': 56}, {'datetime': datetime.datetime(2024, 6, 21, 14, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 1, 'condition': 0, 'lifted_index': -1, 'seeing_percentage': 75, 'temperature': 26.7, 'transparency_percentage': 55, 'humidity': 62}, {'datetime': datetime.datetime(2024, 6, 21, 15, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 16, 'lifted_index': -6, 'seeing_percentage': 75, 'temperature': 24.4, 'transparency_percentage': 55, 'humidity': 74}, {'datetime': datetime.datetime(2024, 6, 21, 16, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 0, 'lifted_index': -6, 'seeing_percentage': 75, 'temperature': 23.7, 'transparency_percentage': 55, 'humidity': 79}, {'datetime': datetime.datetime(2024, 6, 21, 17, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 47, 'lifted_index': -6, 'seeing_percentage': 75, 'temperature': 22.3, 'transparency_percentage': 55, 'humidity': 90}, {'datetime': datetime.datetime(2024, 6, 21, 18, 0, tzinfo=datetime.timezone.utc), 'cloudless_percentage': 0, 'condition': 42, 'lifted_index': 2, 'seeing_percentage': 75, 'temperature': 21.6, 'transparency_percentage': 0, 'humidity': 95}]
icon: mdi:hours-24
friendly_name: Astroweather hourly Forecast

Try:

... + [dict(datetime=entry['datetime'].isoformat(), ...
--------------------------------------^^^^^^^^^^^^

Alternatively, exclude the entity from the recorder and logbook to avoid getting the message.

Wow, thanks a lot for your fast answer! This was exactly what was causing the problem…

1 Like