AndreKR
September 26, 2021, 1:24am
1
In a template, when I print the state
of a weather object, I get something like "partlycloudy"
:
{{ states.weather.foo.state }}
partlycloudy
But if I put the weather in a weather-forcecast
card, it shows “Partly cloudy”.
Where does that string “Partly cloudy” come from and can I get it in a template?
nickrout
(Nick Rout)
September 26, 2021, 1:36am
2
From translations/backend/en.json
AndreKR
September 26, 2021, 2:03am
3
I see, so my question is basically a duplicate of Get translated entity state (in component template) .
nickrout
(Nick Rout)
September 26, 2021, 3:17am
4
Yes it does seem so. How to do what you want, I don’t know sorry.
finity
September 26, 2021, 3:29am
5
Maybe it would be better if you told us what you are actually trying to accomplish. there may be another way to do what you want.
AndreKR
September 26, 2021, 3:32am
6
What do you mean? I’m writing a template (text to be sent to TTS) and it says for example “partlycloudy”, which sounds weird, where it should say whatever the weather card shows (like “Partly cloudy”).
finity
September 26, 2021, 3:44am
7
AndreKR:
What do you mean?
That’s actually what I mean.
I doubt there is any easy way to do it but you could set up the template to use a map from the actual state to the desired words to use in the TTS.
But that would require you to know all of the possible states of the entity.
Here is an example of one I use to set a weather icon:
value_template: >
{% set mapper_icon = {'partly-cloudy-night': 'night-partly-cloudy'} %}
{% set mapper_ds =
{'sunny': 'sunny',
'clear-night': 'night',
'rainy': 'rainy',
'snowy': 'snowy',
'snowy-rainy': 'snowy-rainy',
'windy': 'windy',
'fog': 'fog',
'cloudy': 'cloudy',
'partlycloudy': 'partly-cloudy',
'hail': 'hail',
'lightning':'lightning'} %}
{% set icon = states('sensor.dark_sky_icon') %}
{% set dark = states('weather.dark_sky') %}
{% set weather = mapper_icon[icon] if icon in mapper_icon else
mapper_ds[dark] if dark in mapper_ds else 'sunny-alert' %}
mdi:weather-{{weather}}
Or you could just use a simple if-elif-else template.
another example:
value_template: '{% if is_state("sensor.dark_sky_icon","clear-day") %} Clear
{% elif is_state("sensor.dark_sky_icon","clear-night") %} Clear
{% elif is_state("sensor.dark_sky_icon","rain") %} Rain
{% elif is_state("sensor.dark_sky_icon","snow") %} Snowy
{% elif is_state("sensor.dark_sky_icon","fog") %} Foggy
{% elif is_state("sensor.dark_sky_icon","sleet") %} Sleet
{% elif is_state("sensor.dark_sky_icon","wind") %} Windy
{% elif is_state("sensor.dark_sky_icon","cloudy") %} Cloudy
{% elif is_state("sensor.dark_sky_icon","partly-cloudy-day") %} Partly Cloudy
{% elif is_state("sensor.dark_sky_icon","partly-cloudy-night") %} Partly Cloudy
{% elif is_state("sensor.dark_sky_icon","hail") %} Hailing
{% elif is_state("sensor.dark_sky_icon","lightning") %} Lightning
{% elif is_state("sensor.dark_sky_icon","thunderstorm") %} Thunderstorm
{% endif %}'
nickrout
(Nick Rout)
September 26, 2021, 3:47am
8
Yes, as pointed out in the other thread that is viable, but not across a lot of entities across a number of domains, which is a pity as the work has already been done somewhere in the code
2 Likes
finity
September 26, 2021, 3:49am
9
Sorry, I didn’t click the link.
nickrout
(Nick Rout)
September 26, 2021, 4:54am
10
No problem. Looking at your mapper example, I think the translation file (found here https://github.com/home-assistant/frontend/tree/dev/translations/backend ) could be used as a basis for a map. I don’t know if you can import json into a template though.
Since I ended up using the template provided above and the referenced backend translation file to create my template, I figured I would share it here:
{% set weather_types =
{"clear-night": "Clear Night",
"cloudy": "Cloudy",
"exceptional": "Exceptional",
"fog": "Fog",
"hail": "Hail",
"lightning": "Lightning",
"lightning-rainy": "Lightning & Rainy",
"partlycloudy": "Partly Cloudy",
"pouring": "Pouring",
"rainy": "Rainy",
"snowy": "Snowy",
"snowy-rainy": "Snowy & Rainy",
"sunny": "Sunny",
"windy": "Windy",
"windy-variant": "Windy"} %}
{% set current_weather = states('weather.openweathermap') %}
{% set weather = weather_types[current_weather] if current_weather in weather_types else states('weather.openweathermap') %}
{{weather}}
If there is not a match, I left the raw weather type to let me know I need to add a key: value to the array (and that it’s probably not sunny )