找回密码
 立即注册

微信扫码登录

搜索
查看: 65|回复: 1

[技术探讨] 对Mr.G和dscao大佬和风天气插件的nodered触发修改的自动化

[复制链接]

100

主题

349

回帖

2096

积分

金牌会员

积分
2096
金钱
1647
HASS币
0
发表于 昨天 19:48 | 显示全部楼层 |阅读模式
本帖最后由 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

回复

使用道具 举报

100

主题

349

回帖

2096

积分

金牌会员

积分
2096
金钱
1647
HASS币
0
 楼主| 发表于 昨天 19:53 | 显示全部楼层
自此不需要创建文本,也不需要创建多余的实体,直接让weather.tian_qi来触发,很多人都说nodered好用,我本来也想要研究,但是后来在deepseek找到了归属,感觉什么代码只要你给足了实体名字,和你需要的功能,给足ai,再经过调试总能给你出具很好的程序。浴室就更加没有学习node red的欲望了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian ( 晋ICP备17001384号-1 )

GMT+8, 2025-12-27 06:23 , Processed in 0.121234 second(s), 9 queries , MemCached On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表