|
|
本帖最后由 norikohxb 于 2025-12-26 19:50 编辑
根据论坛dscao大佬和风天气插件写的NR推送播报天气,11.16更新
https://bbs.hassbian.com/thread-21814-1-1.html
(出处: 『瀚思彼岸』» 智能家居技术论坛)
之前都用Mr.G的nodered这个自动化,然后某天HA升级以后!还是tts升级还是dlna升级了,我的nodered就无法推送音箱播放了
也懒得去研究了本来就对node red没有研究,加上AI盛行以后都和很容易让ai给我编程,
把模版给ai
template: - sensor: - name: 一小时天气预警 state: '{% if ("雨" in states.weather.XXXX.attributes.hourly_forecast[0].text) %}{{states.weather.XXXX.attributes.hourly_forecast[0].datetime}}降雨概率为:{{states.weather.XXXX.attributes.hourly_forecast[0].probable_precipitation}}%,可能{{states.weather.XXXX.attributes.hourly_forecast[0].text}},请多加注意!{% else %}未来一小时无不良天气{% endif %}' - name: 自然灾害预警最新一条 state: '{% if (states.weather.XXXX.attributes.warning) %}{{ states.weather.XXXX.attributes.warning[0].pubTime}}{% else %}当前无自然灾害预警信息{% endif %}' attributes: title: "{% if (states.weather.XXXX.attributes.warning) %}{{ states.weather.XXXX.attributes.warning[0].title}} {% else %}null{% endif %}" text: "{% if (states.weather.XXXX.attributes.warning) %}{{ states.weather.XXXX.attributes.warning[0].text }} {% else %}null{% endif %}" pubTime: "{% if (states.weather.XXXX.attributes.warning) %}{{ states.weather.XXXX.attributes.warning[0].pubTime}} {% else %}null{% endif %}" startTime: "{% if (states.weather.XXXX.attributes.warning) %}{{ states.weather.XXXX.attributes.warning[0].startTime}} {% else %}null{% endif %}" endTime: "{% if (states.weather.XXXX.attributes.warning) %}{{ states.weather.XXXX.attributes.warning[0].endTime}} {% else %}null{% endif %}" sender: "{% if (states.weather.XXXX.attributes.warning) %}{{ states.weather.XXXX.attributes.warning[0].sender}} {% else %}null{% endif %}" severity: "{% if (states.weather.XXXX.attributes.warning) %}{{ states.weather.XXXX.attributes.warning[0].severity}} {% else %}null{% endif %}" severityColor: "{% if (states.weather.XXXX.attributes.warning) %}{{ states.weather.XXXX.attributes.warning[0].severityColor}} {% else %}null{% endif %}" level: "{% if (states.weather.XXXX.attributes.warning) %}{{ states.weather.XXXX.attributes.warning[0].level}} {% else %}null{% endif %}" typeName: "{% if (states.weather.XXXX.attributes.warning) %}{{ states.weather.XXXX.attributes.warning[0].typeName}} {% else %}null{% endif %}" all: "{% if (states.weather.XXXX.attributes.warning) %}{{ states.weather.XXXX.attributes.warning}} {% else %}null{% endif %}"
让他分析以后给我了自动化,这样就不需要在configuration.yaml创建多余的这两个实体了,直接在weather.tian_qi推送的时候直接推送音箱tts
程序如下,直接复制到自动化里面保存即可
自己修改音箱参数
alias: 未来一小时不良天气实时播报
description: 当检测到未来一小时有不良天气且在当前时间6:00-23:00时播报
triggers:
- entity_id: weather.tian_qi
attribute: hourly_forecast
trigger: state
conditions:
- condition: template
value_template: >
{# 检查当前时间是否在6:00到23:00之间 #} {% set now_time = now().strftime('%H:%M') %}
{% set in_time = now_time >= '06:00' and now_time <= '23:00' %}
{# 检查是否有hourly_forecast数据 #} {% set forecast =
state_attr('weather.tian_qi', 'hourly_forecast') %} {% set has_forecast =
forecast is defined and forecast|length > 0 %}
{# 检查第一个小时预报是否包含不良天气 #} {% set has_bad_weather = false %} {% if
has_forecast %}
{% set first_hour = forecast[0] %}
{% if first_hour.text is defined %}
{% set has_bad_weather = ("雨" in first_hour.text or "雪" in first_hour.text or
"雹" in first_hour.text or "雷" in first_hour.text or
"雾" in first_hour.text) %}
{% endif %}
{% endif %}
{# 返回最终判断结果 #} {{ in_time and has_forecast and has_bad_weather }}
actions:
- variables:
forecast_data: >
{% set forecast = state_attr('weather.tian_qi', 'hourly_forecast') %} {{
forecast[0] if forecast and forecast|length > 0 else {} }}
message_content: |
{% set forecast = forecast_data %} {% if forecast %}
{% set dt = forecast.datetime %}
{% set hour = dt.split(" ")[1].split(":")[0] if " " in dt else "未知时间" %}
{% set prob = forecast.probable_precipitation if forecast.probable_precipitation is defined else "未知" %}
{% set text = forecast.text if forecast.text is defined else "未知天气" %}
播报新推送的未来一小时的不良天气预告:{{ hour }}点,{{ text }},降雨概率{{ prob }}%。
{% else %}
播报新推送的未来一小时的不良天气预告。
{% endif %}
- target:
entity_id: tts.edge_tts
data:
cache: true
media_player_entity_id: media_player.gong_zheng_dong_lou_yin_xiang
message: "{{ message_content }}"
language: zh-CN
action: tts.speak
mode: single
第二个是自然灾害的推送
alias: 自然灾害预警实时播报
description: 当检测到自然灾害预警信息时立即播报
triggers:
- entity_id: weather.tian_qi
attribute: warning
trigger: state
conditions:
- condition: template
value_template: >
{# 检查当前时间是否在6:00到23:00之间 #} {% set now_time = now().strftime('%H:%M') %}
{% set in_time = now_time >= '06:00' and now_time <= '23:00' %}
{# 检查是否有预警信息 #} {% set warning = state_attr('weather.tian_qi', 'warning')
%} {% set has_warning = warning is defined and warning|length > 0 %}
{# 返回最终判断结果 #} {{ in_time and has_warning }}
actions:
- variables:
warning_data: >
{% set warning = state_attr('weather.tian_qi', 'warning') %} {{
warning[0] if warning and warning|length > 0 else {} }}
message_content: |
{% set warning = warning_data %} {% if warning %}
{# 清理预警文本 #}
{% set warning_text = warning.text | replace("\n", "。") | replace(">", "") | trim %}
{% set warning_title = warning.title | default("自然灾害预警") %}
{% set warning_type = warning.typeName | default("") %}
紧急播报:{{ warning_title }}。
{% if warning_type %}
预警类型:{{ warning_type }}。
{% endif %}
{{ warning_text }}
{% else %}
播报自然灾害预警信息。
{% endif %}
- target:
entity_id: tts.edge_tts
data:
cache: true
media_player_entity_id: media_player.gong_zheng_dong_lou_yin_xiang
message: "{{ message_content }}"
language: zh-CN
action: tts.speak
mode: single
|
|