彩云天气极端天气预警提醒的自动化示例分享
本帖最后由 dscao 于 2023-8-10 22:12 编辑如果使用node-red,可参考: 极端天气预警的node-red推送(需安装彩云天气或和风天气)
--------------------------------------------
经过多次优化修改,最新的解决了之前遇到的问题,相对比较完美了。
------------------------------------------
修复从无到1条时出错,导致漏掉推送的问题。
改为倒序,简化代码。
------------------------------------------
前段时间彩云天气,修复了极端天气预警。https://github.com/fineemb/Colorfulclouds-weather
修改了一下显示,发现如果能推送消息就更好了。
于是做了个自动化,经过多次修改最终效果如下:
自动化UI界面,以YAML编辑
已处理问题:
有时更新预警信息时,原来是两条,更新后还是两条,只是第二条的内容换了。也可能是第一条的内容消失,第二条变第一条,新的补充到第二条。
数据变动触发的情况下判断数据条数,如果减少则不用推送消息。发现有时由两条信息变成一条新信息,造成遗漏,删除或禁用这个环境条件判断即可。示例已删除多余的这个环境条件。
有时有两条信息,一直会第一条与第二条会交换显示,实际没有新内容,也会导到重复推送。
需要对数据以发布时间进行排序后取最后一条来判断及发送。
如果一次出现两条信息,会遗漏时间早一点的消息推送,好在一般没遇到过同时发布两条新预警的。
最后已知问题终于解决。
参考代码:
只需替换觖发条件中的天气实体(彩云天气),在YAML编辑中粘贴,再返回UI中修改。
alias: 彩云天气预警提醒
description: ""
trigger:
- platform: state
entity_id:
- weather.chi_zhou_shi_tai
- weather.gui_chi
attribute: forecast_alert
enabled: true
condition:
- condition: template
value_template: >-
{%if trigger.to_state.attributes.forecast_alert['content']|length>0
%}true{%endif%}
- condition: template
value_template: >-
{%if trigger.from_state.attributes.forecast_alert['content']|length == 0
or
(trigger.to_state.attributes.forecast_alert['content']|sort(attribute='pubtimestamp',
reverse = True))['alertId'] [-14:]|int>
(trigger.from_state.attributes.forecast_alert['content']|sort(attribute='pubtimestamp',
reverse = True))['alertId'][-14:]|int %}true{%endif%}
enabled: true
action:
- service: notify.mobile_app_iphone13_dscao
data:
message: >-
{{trigger.to_state.name}}天气异常警告:{{(trigger.to_state.attributes.forecast_alert['content']|sort(attribute='pubtimestamp',
reverse = True))['description'] }}
title: >-
{{(trigger.to_state.attributes.forecast_alert['content']|sort(attribute='pubtimestamp',
reverse = True))['title'] }}
- service: notify.dingtalk
data:
message: >-
{{trigger.to_state.name}}天气异常警告:{{(trigger.to_state.attributes.forecast_alert['content']|sort(attribute='pubtimestamp',
reverse = True))['description'] }}
title: >-
{{(trigger.to_state.attributes.forecast_alert['content']|sort(attribute='pubtimestamp',
reverse = True))['title'] }}
data:
type: sampleMarkdown
enabled: true
- service: notify.wechat
data:
message: >-
{{trigger.to_state.name}}天气异常警告:{{(trigger.to_state.attributes.forecast_alert['content']|sort(attribute='pubtimestamp',
reverse = True))['description'] }}
title: >-
{{(trigger.to_state.attributes.forecast_alert['content']|sort(attribute='pubtimestamp',
reverse = True))['title'] }}
enabled: true
mode: queued
max: 5
配合:https://bbs.hassbian.com/thread-19320-1-1.html 生成的彩云天气实体 替换触发中的 - weather.chi_zhou_shi_tai - weather.wo_de_jia 等即可,其余部分复制过去不用变。
本帖最后由 dscao 于 2023-1-14 16:02 编辑
但发现还是有点问题,内容有更新但预警数量不变时,会漏掉新的预警消息。
经过多次修改,最后的应该没什么问题了。
本帖最后由 yuyuye 于 2023-1-14 11:29 编辑
谢谢楼主分享,这个我也折腾过。
我在上海,直接用彩云集成的预警,不太正常,预警时有时无,还会在市级、区级之间无规律变化。所以预警我还是用回了和风。
然后关于漏预警的问题,你的触发可以监控整个state变化(包括attribute),只要一个触发就行了。
接着按条件执行,类似:{{ trigger.to_state.state|int > 0 and trigger.to_state.state|int >= trigger.from_state.state|int }}是发布预警{{ trigger.to_state.state|int < trigger.from_state.state|int and trigger.from_state.state|int > 0 }}是预警解除。
本帖最后由 dscao 于 2023-1-14 15:58 编辑
yuyuye 发表于 2023-1-14 11:26
谢谢楼主分享,这个我也折腾过。
我在上海,直接用彩云集成的预警,不太正常,预警时有时无,还会在市级、 ...
看来还是要单独创建一个实体辅助判断才行,属性里的值我以为用不了“trigger.to_state”、“trigger.from_state”。
今天发现有些更新预警信息时,原来是两条,更新后还是两条,只是第二条的内容换了。也可能是第一条的内容消失,第二条变第一条,新的补充到第二条。
trigger.to_state.state|int >= trigger.from_state.state|int 这个判断也就不一定准确了。另外这句我用不了,可能我用的集成生成的少了某个实体。
谢谢你的建议和示例,如果trigger.to_state.state是包含属性的,改进一下也许可以。我再研究研究。
trigger.to_state.attributes 就是属性值,这样好像就可以。
优化完成,结果又发现这个相同信息,一直会第一条与第二条会交换显示,实际没有新内容,也会导到重复推送。
最后再加个排序,应该没问题了。
太强了,从这里面学到了好多东西,copy下来,顺便把自己的自动化又更新完善了一遍,谢谢 楼主这个很好,学习了。我是用nodered,注册了一个彩云账号,专门用于调用api检测自定义天气参数。现在好像每天是可以调用1000次。我是每两分钟用http request节点提交查询天气信息。这个天气预警信息返回的是个数组,用函数节点遍历并比较,提取最新的一条。预警信息等级比较多,一般提取较重以上等级。如果在家就在指定时间段内用TTS播报,限制播报次数,外出就通过企业微信推送。 bainiu 发表于 2023-1-16 15:50
楼主这个很好,学习了。我是用nodered,注册了一个彩云账号,专门用于调用api检测自定义天气参数。现在好像 ...
nodered确实更方便,功能也更多。
如果不想用nodered,也可以用自动化实现。 在家和外出可以在通知的那一步增加选择流程,应该也能实现相同需求。 现在是根据天气预报,提前几个小时语音预警,以及出门开门时候播报天气空气信息。 message: >-
现在时间:{{ states("sensor.time") }},室内客厅PM2.5为{{
states("sensor.zm1_b0f893290ad2_pm25") }}、温度为{{
states("sensor.zm1_b0f893290ad2_temperature") }}度、湿度为百分之{{
states("sensor.zm1_b0f893290ad2_humidity") }}。室外PM2.5为{{
state_attr("weather.shi_tai_2","pm25") }},室外温度为{{
states("sensor.shi_wai_wen_du") }}度;室外湿度为百分之{{
states("sensor.shi_wai_shi_du") }};今天气温 {{
(state_attr("weather.shi_tai_2","forecast").templow
|round(0)|string).replace("-", "零下") }} 到 {{
(state_attr("weather.shi_tai_2","forecast").temperature|round(0)|string).replace("-",
"零下") }} 摄氏度。当前 {{
(state_attr("weather.shi_tai_2","temperature")|round(0)|string).replace("-","零下")
}} 摄氏度,空气质量{{ state_attr("weather.shi_tai_2","aqi_description")
}}。今天 {{ state_attr("weather.shi_tai_2","forecast_hourly")
}}。当前 {{ state_attr("weather.shi_tai_2","forecast_minutely")
}}。 {% for state in
state_attr("weather.chi_zhou_tian_qi","suggestion") -%}
{% if state.title=="comf" -%}
{{state.txt}}
{%- endif %}
{%- endfor -%} {% for state in
state_attr("weather.chi_zhou_tian_qi","suggestion") -%}
{% if state.title=="sport" -%}
{{state.txt}}
{%- endif %}
{%- endfor %}
不用模板生成额外的实体,利用彩云天气和和风天气的属性。
message: >-
现在时间:01:40,室内客厅PM2.5为44、温度为9.2度、湿度为百分之52.8。室外PM2.5为43,室外温度为1.3度;室外湿度为百分之69;今天气温
零下1 到 4 摄氏度。当前 0 摄氏度,空气质量良。今天 晴,今天中午11点钟后转多云,其后晴。当前 最近的降雪带在东边129公里外呢。
白天虽然天气晴好,但早晚会感觉偏凉,午后舒适、宜人。天气较好,但考虑天气寒冷,推荐您进行室内运动,户外运动时请注意保暖并做好准备活动。
dscao 发表于 2023-1-17 01:50
不用模板生成额外的实体,利用彩云天气和和风天气的属性。
大佬 彩云免费版的这些内容都有吗