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.
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
).
How can I thanks you.
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!
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.
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(),