Hello everyone. Following a tutorial from Smart Home Junkie, I made Templates to return the expected rainfall per day, using the openweathermap API. however the template explained does not work. This is apparently the part with
{% for daypart in range(0 ,7) %}
This part gives me a problem.
# essai "Météo de la journée selon Smart Home Junkie"
- trigger:
- platform: homeassistant
event: start
- platform: time_pattern
minutes: /20
action:
- service: weather.get_forecast
data:
type: hourly
target:
entity_id:
- weather.openweathermap
response_variable: my_forcast
sensor:
- name: "Météo of the day by Smart Home Junkie"
unique_id: 14485d6d-fb53-4e72-aea6-85f613b015ee
icon: mdi:weather-pouring
unit_of_measurement: mm
state: >
{% set ns = namespace() %}
{% set ns.totalprecipitation = 0 %}
{% for daypart in range(0 ,7) %}
{% set precipitation = my_forcast.forecast[daypart].precipitation %}
{% set precipitation_probability = my_forcast.forecast[daypart].precipitation_probability / 100 %}
{% if precipitation_probability > 0 %}
{% set precipitation = precipitation * precipitation_ probability %}
{% endif %}
{% set ns.totalprecipitation = ns.totalprecipitation + precipitation %}
{% endfor %}
{{ ns.totalprecipitation | float(0) | round(2) }}
Any idea what my code is missing???
I circumvented with the sum of [daypart] by replacing [daypart] with [0],…,[7]
but with the accumulation method with a for loop I do not have the correct sum.
I’m still missing the last value. I think I didn’t quite understand how counting works in the loop.
Indeed, I understand that this is the solution.
but then how should I do to then have the following positions currently 8 to 15, should I ask for 9 to 16??
In your first post, the example contained a small syntax error and the solution was to simply remove the space character.
Afterwards, you added a new requirement to change the time range (from 0,7 to 8,16) and marked your own post as the Solution.
Now when someone else reads your first post and clicks the Solution link, they are led to believe the problem with the example in your first post is that the time range was wrong. That’s misleading for two reasons:
The first post doesn’t even mention the time range you actually want other than what’s shown in the code.
The example fails to perform a multiplication correctly because it contains a syntax error.
For future reference, it’s helpful to everyone (the volunteers who help you and users reading the solution) if the first post explains the desired result and not simply “does not work”.