Error with helper in automation - trace give an error

Hello,

I’m trying to create an automation for light depending on weather and season. Therefore i created an sensor sensor.licht_verzoegerung_jahreszeit

{% set month = now().month %}
{% set season = 
'winter' if month in [12, 1, 2] else
'spring' if month in [3, 4, 5] else
'summer' if month in [6, 7, 8] else
'autumn' %}
{% set weather = states('weather.my.home') %}
{% if season == 'winter' and weather in ['cloudy', 'rainy', 'pouring'] %}
1800
{% elif season == 'summer' and weather in ['sunny', 'clear'] %}
300
{% else %}
900
{% endif %}
unit_of_measurement: "s"
attributes:
season: >
{% set month = now().month %}
{% if month in [12, 1, 2] %}
winter
{% elif month in [3, 4, 5] %}
spring
{% elif month in [6, 7, 8] %}
summer
{% else %}
autumn
{% endif %}
weather: "{{ states('weather.my.home') }}"

But in my automation under trace i get this error message:

Fehler: ValueError: Template error: int got invalid input '900 unit_of_measurement: "s" attributes: season: > autumn weather: "cloudy"' when rendering template '{{ states('sensor.sensor_licht_verzoegerung_jahreszeit') | int }}' but no default was specified

I can't find a solution and need help.

Peter

You placed as a template helper in the GUI?
You can only use a state template if you do that, you can’t create attributes. If you want the season and weather attributes you need to create this template sensor by using YAML config (outside of the GUI).

For the GUI you can use this as a template:

{% set month = now().month %}
{% set season = 
'winter' if month in [12, 1, 2] else
'spring' if month in [3, 4, 5] else
'summer' if month in [6, 7, 8] else
'autumn' %}
{% set weather = states('weather.my_home') %}
{% if season == 'winter' and weather in ['cloudy', 'rainy', 'pouring'] %}
1800
{% elif season == 'summer' and weather in ['sunny', 'clear'] %}
300
{% else %}
900
{% endif %}

Hello StadtAffe,
First this isn’t right…

Second, would you be so kind as to adjusting the format of your code so that we can read it properly & check the YAML spacing, etc. Editing your original is the preferred way. It is very hard for us to tell what is what when the text formatter jumbles everything like that.
You can use the </> button like this… How to format your code in forum posts
OR… Here is an example of how to fix formatting from the site FAQ Page.
How to help us help you - or How to ask a good question.

right, that should be fixed as well. corrected in my post above.

Hey, you are right. I do my best next time :wink:

I changed the weather.my.home for hiding my location.

Greetz
StadtAffe

1 Like

Thank you for your support. I tried this an it works.

Greetz
StadtAffe

You don’t even need that template to get the season. There’s a core Season integration which will return the current day’s season without truncating at the end of the month, like your template does.

You can add the integration, then your template can be much simpler:

{% set season = states('sensor.season') %}
{% set weather = states('weather.my_home') %}
{% if season == 'winter' and weather in ['cloudy', 'rainy', 'pouring'] %}
1800
{% elif season == 'summer' and weather in ['sunny', 'clear'] %}
300
{% else %}
900
{% endif %}
1 Like