Help finding error in yaml

I and not find the error in this yaml code below. Please help. It states there is an unexpected ‘end of statement’ block but I can not find it. Any help would be appreciated.

Logger: homeassistant.config
Source: config.py:864
First occurred: 6:01:53 PM (1 occurrences)
Last logged: 6:01:53 PM

Invalid config for [template]: invalid template (TemplateSyntaxError: unexpected ‘end of statement block’) for dictionary value @ data[‘sensor’][0][‘state’]. Got "{%- set totalprecipitation = 0 | float -%} {%- set ns = namespace() -%} {%- set ns.totalprecipitation = 0 -%} {%- for daypart in range(0,7) -%}\n {%- set precipitation = state_attr(‘weather.openweathermap’, ‘forcast’)[dyapart].precipitation -%}\n {%- set precipitation_probability = state_attr(‘weather.openweathermap’,‘forcast’)[daypart].precipitation_probability / 100% -%}\n {%- if precipitation_probability > 0 -%}\n {%- set precipitation = precipitation * precipitation_probability -%}… (See /config/configuration.yaml, line 11).

sensor:
  ##### Sprinkler Total Expected Precipitation ###################################
  - name: "Sprinkler Total Expected Precipitation"
    unique_id: 1d988d1a-ba4c-496c-ab17-67166a273887
    icon: mdi:weather-pouring
    unit_of_measurement: in
    state: >
      {%- set totalprecipitation = 0 | float -%}
      {%- set ns = namespace() -%}
      {%- set ns.totalprecipitation = 0 -%}
      {%- for daypart in range(0,7) -%}
        {%- set precipitation = state_attr('weather.openweathermap', 'forcast')[dyapart].precipitation -%}
        {%- set precipitation_probability = state_attr('weather.openweathermap','forcast')[daypart].precipitation_probability / 100% -%}
        {%- if precipitation_probability > 0 -%}
          {%- set precipitation = precipitation * precipitation_probability -%}
        {%- endif -%}
        {%- set ns.totalprecipitation = ns.totalprecipitation + precipitation -%}
      {%- endfor -%}
      {{ ns.totalprecipitation | float | round(2)}}

Spelling error:

forcast')[dyapart].precipitation
           ^^

Thank you for that. I fixed the spelling error and I still receive that same error stating “TemplateSystaxError: unexpected ‘end of statement block’)”

Is this your first Template Sensor (in modern format)? Where is it located? In configuration.yaml or in a separate file (like templates.yaml)?

Normally, the configuration of a Template Sensor (in modern format) should be something like this:

template:
  - sensor:
      - name: "Etc"

If all of your Template Sensors are located in a separate file (separate from configuration.yaml) then it will look something like this:

- sensor:
    - name: "Etc"

In your example, it looks like this which is incorrect:

sensor:
  - name: "Etc"

If you have created a Template Sensor under the sensor: key in configuration.yaml then that’s incorrect and is the reason for the error message.

Maybe the use of the percent in “100% -%}” is causing some confusion?

2 Likes

This is my first template sensor. It is in template.yaml.

I figured it out. It was my …’/100% -%}’ that was the issue. Should have been ‘/100 -%}’ The extra % was the syntax error.

1 Like

Just out of interest, how did you know to use the term [daypart] in the template sensor to retrieve the data from the weather report? Is that a common term used for that type of data response?

Is there a corresponding [nightpart], or any other [xxxpart]?

Just trying to learn the whys of these templates that other people are writing.

Thanks.

Is this your code ?
I see the exact code behind a payed wall here