Hi,
Does anyone know how to round the weather forecast to whole degrees in the met.no integration? Talking about these decimals seen below.
The Weather Forecast Card displays the values reported by the met.no integration. Neither the card or the integration support modifications to the reported values.
One way to do what you want is to create a new weather
entity using the Template Weather integration. This new entity can base all of its values on the ones reported by your existing weather
entity (the one using met.no). The values can be rounded in the templates.
The challenging part is creating a template to convert all the values in the forecast
attribute.
Revised version
2024-04-07
Revised version is posted here:
Original version
Try this. You will need to replace “your_weather” with the name of your weather entity.
weather:
- platform: template
name: "Example"
condition_template: "{{ states('weather.your_weather') }}"
temperature_template: "{{ state_attr('weather.your_weather', 'temperature') | round(0, default=-99) }}"
humidity_template: "{{ state_attr('weather.your_weather', 'humidity') | round(0, default=-99) }}"
forecast_template: >
{% set ns = namespace(z = []) %}
{% for x in state_attr('weather.your_weather', 'forecast') %}
{% set ns.z = ns.z +
[{
'condition': x.condition,
'precipitation': x.precipitation,
'temperature': x.temperature | round(0, default=-99),
'templow': x.templow | round(0, default=-99),
'datetime': x.datetime,
'wind_bearing': x.wind_bearing,
'wind_speed': x.wind_speed
}] %}
{% endfor %}
{{ ns.z }}
That is interesting that it requires a new entity.
I have started with the entity you created, but having some issues. The only thing it displays is the current condition. Let me see if I can play with it a bit…
I dropped all the rounding out of your example to see if I could get any data to show, and…it worked perfectly and rounded. Odd.
I don’t know why you are experiencing the problems you described. Removing all references to the round
filter should not cause the values to be rounded (that’s completely counter-intuitive) so there’s clearly something odd with whatever it is that you’ve done.
The example I posted above works when tested with version 2021.10.4. In the following screenshot, the upper weather card represents the original Met.no weather forecast with unmodified values. The lower weather card uses the Template Weather integration I posted showing rounded values.
Perhaps I need to update my version, but with the below it works perfectly.
weather:
- platform: template
name: "myweather_rounded"
condition_template: "{{ states('weather.myweather') }}"
temperature_template: "{{ state_attr('weather.myweather', 'temperature') }}"
humidity_template: "{{ state_attr('weather.myweather', 'humidity') }}"
pressure_template: "{{ state_attr('weather.myweather', 'pressure') }}"
wind_bearing_template: "{{ state_attr('weather.myweather', 'wind_bearing') }}"
wind_speed_template: "{{ state_attr('weather.myweather', 'wind_speed') }}"
forecast_template: >
{% set ns = namespace(z = []) %}
{% for x in state_attr('weather.myweather', 'forecast') %}
{% set ns.z = ns.z +
[{
'condition': x.condition,
'precipitation': x.precipitation,
'temperature': x.temperature,
'templow': x.templow,
'datetime': x.datetime,
'wind_bearing': x.wind_bearing,
'wind_speed': x.wind_speed
}] %}
{% endfor %}
{{ ns.z }}
I can’t comment on the behavior of older versions of Home Assistant, only on the behavior of current versions.
Look at the third Weather card in the following screenshot. That’s how my example appears if all references to round
are removed; there’s no magical rounding. That’s also how your modified example will appear whenever you upgrade to 2021.10.X (or later).
I am updating now, so it should happen soon.
Out of curiosity, which version of Home Assistant are/were you using?
Because I can’t ever recall a version where this:
{{ state_attr('weather.myweather', 'temperature') }}
would produce the same value as this:
{{ state_attr('weather.myweather', 'temperature') | round(0, default=-99) }}
if temperature
is a float value like 22.6
and not already an integer like 22
I was running OS 6.4, just upgraded to 6.6. Core is upgrading now from 2021.8.7.
I am updated to the latest and the code that I pasted above still produces rounded values. I have no idea why. Everything you are saying seems right, but…it works.
Clearly you’re doing something strange somewhere because values don’t magically round themselves. EDIT They can if there’s unit system conversion involved!
Paste this into the Template Editor and you’ll see the difference.
{{ state_attr('weather.your_weather', 'temperature') | round(0, default=-99) }}
{{ state_attr('weather.your_weather', 'temperature') }}
By removing the round
filter from the example I posted, your modified version simply copies the values from the met.no integration unchanged. I demonstrated that in my previous post where the third Weather card shows the same values as in the first Weather card. EDIT Because my system is configured to use the metric system.
Clearly the Weather card doesn’t round the values, otherwise it would have also rounded met.no’s values so something is rounding them.
The clue that you have done something odd is the fact that you originally showed a screenshot where only the current condition was displayed and the temperature was NaN (Not a Number). That’s representative of a misconfiguration somewhere in the card and/or Template Weather. EDIT Possibly due to misconfiguration but the real reason for the “magical rounding” is described below.
Bingo! I found the allegedly “odd” thing you are doing and it’s not all that unusual at all.
Your Home Assistant system is configured to use the Imperial unit system. Met.no’s values are received in metric and converted to imperial. The conversion process rounds the temperature values (but not initially by the met.no integration).
I switched my test instance of Home Assistant from Metric to Imperial. Here are the three Weather cards representing:
- met.no’s original values
- Template Weather with
round
filter - Template Weather without
round
filter
Notice that the second and third cards are both showing rounded values despite the fact the third one does not actually employ the round
filter.
So that mysterious “something” that is magically rounding the values is unit conversion from metric to imperial.
For best compatibility with metric and imperial unit systems, I recommend using the original version I posted that employs the round
filter (because your version without the round
filter won’t do any rounding on a system configured to use metric). Plus, should this automatic-rounding behavior ever be eliminated in the future, my original version will continue to work correctly whereas yours won’t.
Aha! I had thought that it could be related to a conversion at some point. Thanks for all the help, and for sorting out the issue.
And, I confirm that your original code works perfectly fine in the latest version.
Combined the first code with the second one to not lose the pressure and other sensors. All works. Thank you @123
With broken weather template and forecast_template
now deprecated. I’m now getting error with your code that was fine until the latrst HA release. Any suggestions on how to fix it, please
Logger: homeassistant.config
Source: config.py:1324
First occurred: April 4, 2024 at 8:54:01 PM (1 occurrences)
Last logged: April 4, 2024 at 8:54:01 PM
Invalid config for 'weather' from integration 'template' at packages/integrations/weather.yaml, line 7: 'forecast_template' is an invalid option for 'template.weather', check: forecast_template, please check the docs at https://www.home-assistant.io/integrations/template
The easiest fix would be to choose a weather provider that doesn’t require the use of the Template Weather integration.