BlueRoseNote/07-Other/生活/智能家居/HomeAssistant设置天气相关.md

2.0 KiB
Raw Blame History

前言

和风天气 Token

afdc8b37975043888ad47943ff25b980

HA

在configuration.yaml中添加

template:
  - trigger:
      - platform: time_pattern
        hours: "*"
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.he_feng_tian_qi
        response_variable: hourly
    sensor:
      - name: "小时天气预警"
        unique_id: weather_forecast_hourly
        state: >
          {% if hourly['weather.he_feng_tian_qi'].forecast[0].condition in ('sunny','cloudy','partlycloudy','windy') %}
            off
          {% else %}
            on
          {% endif %}
        attributes:
          states: >
            {% if hourly['weather.he_feng_tian_qi'].forecast[0].condition in ('sunny','cloudy','partlycloudy','windy') %}
              未来一小时,天气{{ hourly['weather.he_feng_tian_qi'].forecast[0].text }},没有降雨
            {% else %}
              接下来一小时会有{{ hourly['weather.he_feng_tian_qi'].forecast[0].text }},降水概率为 {{ hourly['weather.he_feng_tian_qi'].forecast[0].precipitation_probability }}%
            {% endif %}

自动化

对应的自动化:

alias: 一小时天气预警
description: ""
triggers:
  - entity_id:
      - sensor.xiao_shi_tian_qi_yu_jing
    trigger: state
actions:
  - device_id: 384bea520a0ee825ecb63fb71d19b2a1
    domain: mobile_app
    type: notify
    message: "注意注意! 接下来的天气为{{state_attr('sensor.xiao_shi_tian_qi_yu_jing','states')}} "
    title: 天气预警
  - action: notify.send_message
    metadata: {}
    data:
      message: "[\"注意注意! {{state_attr('sensor.xiao_shi_tian_qi_yu_jing','states')}}\"]"
    target:
      entity_id: notify.xiaomi_cn_799945688_l06a_play_text_a_5_1
initial_state: true