I have a nice set of templated sensors that use hourly weather information to create per-hour precipitation sensors:
# Weather Hourly
- platform: template
sensors:
rain_hourly_0:
value_template: "{{ state_attr('weather.home_hourly','forecast')[0].precipitation }}"
friendly_name: 'Rain Forecast Hour 0'
unit_of_measurement: 'mm'
attribute_templates:
time_of_day: "{{ as_timestamp(state_attr('weather.home_hourly','forecast')[0].datetime,0) | timestamp_custom('%-I %p',True,None) }}"
icon_template: >-
{% if state_attr('weather.home_hourly','forecast')[0].precipitation|float(default=0) >= 2.0 %}mdi:water
{% elif state_attr('weather.home_hourly','forecast')[0].precipitation|float(default=0) >= 0.2 %}mdi:water-outline
{% else %}mdi:water-remove-outline
{% endif %}
rain_hourly_1:
value_template: "{{ state_attr('weather.home_hourly','forecast')[1].precipitation }}"
friendly_name: 'Rain Forecast Hour 1'
unit_of_measurement: 'mm'
attribute_templates:
time_of_day: "{{ as_timestamp(state_attr('weather.home_hourly','forecast')[1].datetime,0) | timestamp_custom('%-I %p',True,None) }}"
icon_template: >-
{% if state_attr('weather.home_hourly','forecast')[1].precipitation|float(default=0) >= 2.0 %}mdi:water
{% elif state_attr('weather.home_hourly','forecast')[1].precipitation|float(default=0) >= 0.2 %}mdi:water-outline
{% else %}mdi:water-remove-outline
{% endif %}
Works fine, except thereās 24 of them obviouslyā¦ Is there a way to use a loop to create the same sensors? I have working code that produces exactly what I want in the Developer Template:
{% for i in state_attr('weather.home_hourly','forecast') -%}
rain_hourly_{{ loop.index0 }}:
value_template: {{ i.precipitation|float(default=0) }}
friendly_name: "Rain Forecast Hour {{ loop.index0 }}"
unit_of_measurement: 'mm'
attribute_templates:
time_of_day: "{{ as_timestamp(i.datetime,default="unknown") | timestamp_custom('%-I %p',True,None) }}"
icon_template: >-
{% if i.precipitation|float(default=0) >= 2.0 %}mdi:water
{% elif i.precipitation|float(default=0) >= 0.2 %}mdi:water-outline
{% else %}mdi:water-remove-outline
{% endif %}
{% endfor %}
Iām just stumped as to how I would integrate that into my sensors.yaml file.
I tried inserting it as a block of code but, as expected, the yaml parser in the File Editor doesnāt like it:
missed comma between flow collection entries (194:10)
191 | # Weather Hourly
192 | - platform: template
193 | sensors:
194 | {% for i in state_attr('weather. ...
----------------^
195 | rain_hourly_{{ loop.index0 }}:
196 | value_template: {{ i.precipita ...
And the Developer YAML āCheck Configurationā thing hates it too:
Configuration invalid!
Error loading /config/configuration.yaml: while scanning for the next token
found character '%' that cannot start any token
in "/config/sensors.yaml", line 194, column 8
Is there another way to load such a templated set of sensors?