Hi I am trying to setup a template sensor to return some state values to choose a specific image in the pictures elements card. The reason for the template is I want to combine the state of two entities to choose a picture.
I am trying to use the SUN integration and the Weather integration to pull values.
The desired state is than when it is partly cloudy and night pick image X and when partly cloudy and night pick image B
I am not a huge expert in templating learning as I go along, I have come up with this but it does not work in the developer tool template testing area.
Can you help please , thanks in advance
{% set weaf = states('weather.forecast_home')|float %}
{% set sun = states('sun.sun')|float %}
{% if weaf = "sunny" %}WI0
{% elif weaf = "clear_night" %}WI1
{% elif weaf = "cloudy" %}WI2
{% elif weaf = "fog" %}WI3
{% elif weaf = "lightning_rainy" %}WI4
{% elif weaf = "pouring" %}WI5
{% elif weaf = "rainy" %}WI6
{% elif weaf = "snowy" %}WI7
{% elif weaf = "snowy_rainy" %}WI8
{% elif weaf = "partlycloudy" and sun = "below_horizon" %}WI9
{% elif weaf = "partlycloudy" and sun = "above_horizon" %}WI10
{% endif %}
Your comparison’s require 2 equal signs to perform a test, see Template Designer Documentation — Jinja Documentation (3.2.x)
{% if weaf == "sunny" %}WI0
{% elif weaf == "clear_night" %}WI1
etc...
Thank you this has worked
I also tried this and also worked
state: >
{% if is_state('weather.forecast_home', 'sunny') %}
WI0
{% elif is_state('weather.forecast_home', 'clear-night') %}
WI1
{% elif is_state('weather.forecast_home', 'cloudy') %}
WI2
{% elif is_state('weather.forecast_home', 'fog') %}
WI3
{% elif is_state('weather.forecast_home', 'lightning-rainy') %}
WI4
{% elif is_state('weather.forecast_home', 'pouring') %}
WI5
{% elif is_state('weather.forecast_home', 'rainy') %}
WI6
{% elif is_state('weather.forecast_home', 'snowy') %}
WI7
{% elif is_state('weather.forecast_home', 'snowy-rainy') %}
WI8
{% elif is_state('weather.forecast_home', 'partlycloudy') and is_state('sun.sun', 'below_horizon') %}
WI9
{% elif is_state('weather.forecast_home', 'partlycloudy') and is_state('sun.sun', 'above_horizon') %}
WI10
{% endif %}
In addition to the ==
issue, your variable set
statements are incorrect.
The purpose of the float
filter function is to convert numeric strings into floating point numbers. Neither of the sensors you are applying float
to return numeric strings and since you have not provided a default value, they will both cause the template to fail due to error.
FWIW, I would use a dictionary instead of a series of if’s:
{% set weaf = states('weather.forecast_home') %}
{%- set mapper = {
"clear_night": "WI1",
"cloudy": "WI2",
"fog": "WI3",
"lightning_rainy": "WI4",
"pouring": "WI5",
"rainy": "WI6",
"snowy": "WI7",
"snowy_rainy": "WI8"} %}
{%- if weaf == "partlycloudy" %}
{{ "WI9" if states('sun.sun') == "below_horizon" else "WI10" }}
{%- else %}
{{ mapper.get(weaf)}}
{%- endif %}
1 Like
Thank you that is also a neat solution
Vas