What I was looking for isn’t possible see responses
Hi
I’ve followed some examples to create sensors using a template to loop an array.
It works when I test my template in Developer Tools
But when I drop the template in configuration.yaml
I got an error
Error loading /config/configuration.yaml: while scanning for the next token
found character '%' that cannot start any token
pointing the first % in {%- for allergen
I understand it’s a conflit between yaml and templating syntax but I cannot find how to correct that.
Previous questions on the forum where more on using value_template: >
but in my case sensors: >
isn’t the answer.
Any clue or working example ?
sensor:
- platform: rest
name: Pollens
resource: https://www.pollens.fr/risks/thea/counties/11
json_attributes: ['risks', 'riskLevel', 'countyName']
value_template: '{{ value_json.riskLevel }}'
# Request every 6 hours
scan_interval: 21600
- platform: template
sensors:
{%- for allergen in state_attr('sensor.pollens', 'risks') %}
{{ state_attr('sensor.pollens', 'countyName') | lower }}_allergen_{{ loop.index }}:
friendly_name: {{ state_attr('sensor.pollens', 'countyName') }} {{ allergen.pollenName }}
value_template: {{ allergen.level }}
{%- endfor %}
This is the value returned from the rest call :
{"countyName":"Jura","countyNumber":"39","risks":[{"pollenName":"Tilleul","level":0},{"pollenName":"Ambroisies","level":0},{"pollenName":"Olivier","level":0},{"pollenName":"Plantain","level":2},{"pollenName":"Noisetier","level":0},{"pollenName":"Aulne","level":0},{"pollenName":"Saule","level":0},{"pollenName":"Cypr\u00e8s","level":0}],"riskLevel":4}