Rounding Forecast with Weather integration met.no

Here’s a revised version of what I had suggested two years ago.

Create a Trigger-based Template Sensor to periodically get your daily weather forecast and save it in an attribute named forecast. In the following example, replace weather.forecast_home with the entity_id of your weather entity.

template:
  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: event
        event_type: event_template_reloaded
    action:
      - variables:
          entity_id: weather.forecast_home
      - service: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: "{{ entity_id }}"
        response_variable: daily
    sensor:
      - name: Daily Weather Forecast
        unique_id: daily_weather_forecast
        state: "{{ now().isoformat() }}"
        attributes:
          forecast: >
            {% set ns = namespace(forecast=[]) %}
            {% for d in daily[entity_id]['forecast'] %}
            {% set ns.forecast = ns.forecast +
            [{
              "condition": d.condition,
              "datetime": d.datetime,
              "wind_bearing": d.wind_bearing,
              "temperature": d.temperature | round(0, default=-99),
              "templow": d.templow | round(0, default=-99),
              "wind_speed": d.wind_speed | round(0, default=-99),
              "precipitation": d.precipitation,
              "humidity": d.humidity
            }] %}
            {% endfor %}
            {{ ns.forecast }}

After you have added it to wherever you store Template entities (either in configuration.yaml or perhaps in a separate file like templates.yaml) execute Developer Tools > YAML > Reload Template entities. Confirm there are no related errors reported in the Log.

Confirm sensor.daily_weather_forecast exists in Developer Tools > States and it contains a forecast attribute.

Now add the following Template Weather entity to your configuration.yaml file. Change name to whatever you prefer and replace weather.forecast_home with the entity_id of your weather entity.

weather:
  - platform: template
    name: "Forecast Home Rounded"
    condition_template: "{{ states('weather.forecast_home') }}"
    temperature_template: "{{ state_attr('weather.forecast_home', 'temperature') | round(0, default=-99) }}"
    humidity_template: "{{ state_attr('weather.forecast_home', 'humidity') | round(0, default=-99) }}"
    forecast_daily_template: "{{ state_attr('sensor.daily_weather_forecast', 'forecast') }}"

After you have added it, execute Developer Tools > YAML > Reload Template entities (or restart Home Assistant). Confirm there are no related errors reported in the Log.

Confirm weather.forecast_home_rounded (or whatever you renamed it to) exists in Developer Tools > States.

Now you can use the Weather Forecast card to display the new weather entity. In the following screenshot, the top weather card shows the original values from met.no (weather.forecast_home) and the bottom weather card shows rounded values (weather.forecast_home_rounded).

image

2 Likes

How can I thanks you. :clap:
Not sure why the default card behaviour or sensor doesn’t allow rounding. Who would want temp to be decimals… I wish this will be addressed in some enhancement.

Thanks again!

3 Likes

Hi @123

You guide is nice and looks very well. I got to the similar results. Unfortunately I cannot make it work the way you do.
Even if I copy your code directly I am not getting the forecast visible in States or Weather Card.

First is already the sensor which is not shown nicely structured as yours:

And then no forecast in the weather template entity:

On the other hand, the original entity, before rounding, is fine:

Any idea what can be messed up?

I think forecast as an attribute was removed for the weather template entity… it doesn’t show up any more in my testing either.

Well, it is a bit more complicated. The template for hourly and daily forecast seems to be working.
But not with all weather entities. At least my current impression.

See here https://community.home-assistant.io/t/new-weather-forecast-template/622642/52?u=tristone

You may be correct … I actually looked at the changes made to the HA template weather.py, and they removed legacy “forecast” but they kept “daily”, “hourly” and “twice_daily” forecasts.

The template weather entity didn’t like my “daily” forecast from my sensor but at least told me why…that it didn’t support a particular key in the daily forecast (doesn’t support “detailed_description”).
The template weather entity hasn’t complained about my “hourly” forecast from my sensor, but the hourly forecast for some reason doesn’t show up as an attribute in my template weather entity.

Which weather integration are you using?

The reason why I am asking is because it’s reporting dates as datetime objects instead of datetime strings.

1 Like

Custom one, for Czech Hydrometeorological Institute

https://github.com/kukulich/home-assistant-aladin-online

Interesting is that if I use the get_forecasts service the output looks good to me, just timezone is missing:

service: weather.get_forecasts
data:
  type: hourly
target:
  entity_id:
    - weather.domov
    - weather.openweathermap
weather.domov:
  forecast:
    - datetime: "2024-04-09T11:00:00"
      condition: sunny
...
weather.openweathermap:
  forecast:
    - condition: partlycloudy
      precipitation_probability: 0
      datetime: "2024-04-09T12:00:00+00:00"

The weather.domov (custom integration) is not working in the sensor, the weather.openweathermap is working.

I’ll try to pass the message (object vs. string) to the developer, we are in touch already.

So far I have seen two reports of the problem you have described and both were due to the use of a custom weather integration whose dates are reported as datetime objects.

Your hint helped.
Problem in the integration is solved now.

Changed this:

			self._forecast.append({
				ATTR_FORECAST_TIME: hourly_forecast.datetime,

To this:

			self._forecast.append({
				ATTR_FORECAST_TIME: hourly_forecast.datetime.isoformat(),