播报的内容那里我也单独弄了个sensor比较省事。
cyweather_rainfall_intensity_in_03_hour_new:
value_template: >
{# 定义LIST #}
{% set rainfall = namespace(time1="0", time2="0", data1="无", data2="无") %}
{% set skyconList = [states.sensor.cyweatherforecast.attributes.result.hourly.skycon[0].value, states.sensor.cyweatherforecast.attributes.result.hourly.skycon[1].value, states.sensor.cyweatherforecast.attributes.result.hourly.skycon[2].value] %}
{% set skyconKey1List = ["LIGHT_RAIN","MODERATE_RAIN","HEAVY_RAIN","STORM_RAIN","LIGHT_SNOW","MODERATE_SNOW","HEAVY_SNOW","STORM_SNOW"] %}
{% set skyconKey2List = ["小雨","中雨","大雨","暴雨","小雪","中雪","大雪","暴雪"] %}
{# 循环判断123小时预报的天气,保存第一次的异常天气和最后一次的异常天气 #}
{% for skycon in skyconList %}
{% set outer_loop = loop %}
{% for skyconKey1 in skyconKey1List %}
{% if skycon == skyconKey1 %}
{% if rainfall.time1 == "0" %}
{% set rainfall.time1 = outer_loop.index %}
{% set rainfall.data1 = skyconKey2List[loop.index-1] %}
{% else %}
{% set rainfall.time2 = outer_loop.index %}
{% set rainfall.data2 = skyconKey2List[loop.index-1] %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
{# 无异常天气就返回无 #}
{% if rainfall.time1 == "0" or rainfall.time1 == "0" %}
{{- "无" -}}
{% else %}
{# 编辑第一次的异常天气 #}
{{- rainfall.time1 | string + "小时内会下" + rainfall.data1 -}}
{# 如果最后一次异常天气和第一次不同,就继续播报天气变化 #}
{%- if rainfall.time1 | int < rainfall.time2 | int and rainfall.data1 != rainfall.data2 %}
{{- ",并在之后" + (rainfall.time2 - rainfall.time1) | string + "小时内变为" + rainfall.data2 -}}
{% endif -%}
{% endif %}
|