Code issues in a template for weather with a loop

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]

Thank you for your suggestions.

Stop duplicating posts please. Wait for someone to reply to this post. Thank you.

{% set precipitation = precipitation * precipitation_ probability %}
                                                     ^
                                                     |
                                          Remove this space.

thanks you for your response, it was really my error.

I still have a problem.
the values are counted from 0 to 7 to obtain the sum of 8 values, the first being counted 0.

# Météo Pluviométrie cumulées 0-24H
- 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: daily
  sensor:
    - name: "Météo Pluviométrie cumulées 0-24H"
      unique_id: 1039cdde-c8bf-4e62-b18b-88285ef1cb07
      state: >
        {{ ( (state_attr('weather.openweathermap','forecast')[0].precipitation | float(default=0) )
        + (state_attr('weather.openweathermap','forecast')[1].precipitation | float(default=0) )
        + (state_attr('weather.openweathermap','forecast')[2].precipitation | float(default=0) )
        + (state_attr('weather.openweathermap','forecast')[3].precipitation | float(default=0) )
        + (state_attr('weather.openweathermap','forecast')[4].precipitation | float(default=0) )
        + (state_attr('weather.openweathermap','forecast')[5].precipitation | float(default=0) )
        + (state_attr('weather.openweathermap','forecast')[6].precipitation | float(default=0) )
        + (state_attr('weather.openweathermap','forecast')[7].precipitation | float(default=0) ) ) | round(2) }}
        {{ states('weather.openweathermap') }}
      availability: "{{ has_value('weather.openweathermap') }}"
      icon: >-
        {% if has_value('weather.openweathermap') %}
        {% if daily.forecast[0].condition == 'partlycloudy' %}
          mdi:weather-partly-cloudy
        {% elif daily.forecast[0].condition == 'clear-night' %}
          mdi:weather-night
        {% elif daily.forecast[0].condition == 'exceptionnal' %}
          mdi:alert-circle-outline
        {% else %}
          {{ 'mdi:weather-' }}{{ daily.forecast[0].condition }}
        {% endif %}
        {% endif %}

with this method I have the correct sum of 8 values.

But with this code:

# "Météo Pluviométrie cumulées 0-24H 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 Pluviométrie cumulées 0-24H Junkie"
      unique_id: aed23e70-d00e-4058-9c7c-b17877c1dd84
      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 ns.totalprecipitation = ns.totalprecipitation + precipitation %}
        {% endfor %}
        {{ ns.totalprecipitation |  float(0) | round(2) }}

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.

use range(8)

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??

the correct answer seems to be:

{% for daypart in range( 8 , 16 ) %}

I don’t really understand the logic of how to count.
could you explain to me the correct reasoning.

The jinja document explains range and what each argument means

Merci pour le conseil, tout est résolu.

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:

  1. The first post doesn’t even mention the time range you actually want other than what’s shown in the code.
  2. 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”.