🏠

HomeAssistantにYahoo! JAPAN(天気予報API)の降水量データを表示する

2024/10/13に公開

はじめに

Home Assistantのダッシュボードを作成した際に自身で工夫したエンティティの設定方法を記事化しました。
この記事ではYahoo! JAPANの気象情報API情報を利用し、降水量情報を取り込む設定を説明します。

気象情報API

以下はリファレンスから抜粋したものです。
APIの呼び出しにはClient IDを事前に用意する必要があります。

概要

・指定した緯度経度の雨の強さを取得できるAPIです。
・現在時刻の降水強度実測値から、60分後までの降水強度予測値を取得できます。

利用制限

気象情報APIは、24時間中1アプリケーションにつき50000件のリクエストが上限となっています。詳しくは「利用制限」をご参照ください。

Client ID(アプリケーションID)

Client IDは、Yahoo!デベロッパーネットワークが提供するAPIの使用状況を把握し、不正行為を防ぐために使用されます。第三者に開示したり、第三者が参照できる場所に記載しないでください。

設定

configuration.yaml > rest:

configuration.yaml
# Yahoo Japan 気象情報API
# https://developer.yahoo.co.jp/webapi/map/openlocalplatform/v1/weather.html#:~:text=Yahoo!%E3%83%87%E3%83%99%E3%83%AD%E3%83%83
# resourceの {経度}、{緯度}、{Client ID} は要置換
  - scan_interval: 180
    resource: 'https://map.yahooapis.jp/weather/V1/place?coordinates={経度},{緯度}&appid={Client ID}&output=json'
    sensor:
      - name: yweather_min_0_precipitation
        unique_id: yweather_min_0_precipitation
        value_template:  >-
          {% if value_json.Feature[0].Property.WeatherList.Weather[0] %}
            {{value_json.Feature[0].Property.WeatherList.Weather[0].Rainfall}}
          {% endif %}
        device_class: precipitation
        unit_of_measurement: mm
      - name: yweather_min_0_date
        unique_id: yweather_min_0_date
        value_template:  >-
          {% if value_json.Feature[0].Property.WeatherList.Weather[0] %}
            {{value_json.Feature[0].Property.WeatherList.Weather[0].Date}}
          {% endif %}
      - name: yweather_min_1_precipitation
        unique_id: yweather_min_1_precipitation
        value_template:  >-
          {% if value_json.Feature[0].Property.WeatherList.Weather[1] %}
            {{value_json.Feature[0].Property.WeatherList.Weather[1].Rainfall}}
          {% endif %}
        device_class: precipitation
        unit_of_measurement: mm
      - name: yweather_min_1_date
        unique_id: yweather_min_1_date
        value_template:  >-
          {% if value_json.Feature[0].Property.WeatherList.Weather[1] %}
            {{value_json.Feature[0].Property.WeatherList.Weather[1].Date}}
          {% endif %}
      - name: yweather_min_2_precipitation
        unique_id: yweather_min_2_precipitation
        value_template:  >-
          {% if value_json.Feature[0].Property.WeatherList.Weather[2] %}
            {{value_json.Feature[0].Property.WeatherList.Weather[2].Rainfall}}
          {% endif %}
        device_class: precipitation
        unit_of_measurement: mm
      - name: yweather_min_2_date
        unique_id: yweather_min_2_date
        value_template:  >-
          {% if value_json.Feature[0].Property.WeatherList.Weather[2] %}
            {{value_json.Feature[0].Property.WeatherList.Weather[2].Date}}
          {% endif %}
      - name: yweather_min_3_precipitation
        unique_id: yweather_min_3_precipitation
        value_template:  >-
          {% if value_json.Feature[0].Property.WeatherList.Weather[3] %}
            {{value_json.Feature[0].Property.WeatherList.Weather[3].Rainfall}}
          {% endif %}
        device_class: precipitation
        unit_of_measurement: mm
      - name: yweather_min_3_date
        unique_id: yweather_min_3_date
        value_template:  >-
          {% if value_json.Feature[0].Property.WeatherList.Weather[3] %}
            {{value_json.Feature[0].Property.WeatherList.Weather[3].Date}}
          {% endif %}
      - name: yweather_min_4_precipitation
        unique_id: yweather_min_4_precipitation
        value_template:  >-
          {% if value_json.Feature[0].Property.WeatherList.Weather[4] %}
            {{value_json.Feature[0].Property.WeatherList.Weather[4].Rainfall}}
          {% endif %}
        device_class: precipitation
        unit_of_measurement: mm
      - name: yweather_min_4_date
        unique_id: yweather_min_4_date
        value_template:  >-
          {% if value_json.Feature[0].Property.WeatherList.Weather[4] %}
            {{value_json.Feature[0].Property.WeatherList.Weather[4].Date}}
          {% endif %}
      - name: yweather_min_5_precipitation
        unique_id: yweather_min_5_precipitation
        value_template:  >-
          {% if value_json.Feature[0].Property.WeatherList.Weather[5] %}
            {{value_json.Feature[0].Property.WeatherList.Weather[5].Rainfall}}
          {% endif %}
        device_class: precipitation
        unit_of_measurement: mm
      - name: yweather_min_5_date
        unique_id: yweather_min_5_date
        value_template:  >-
          {% if value_json.Feature[0].Property.WeatherList.Weather[5] %}
            {{value_json.Feature[0].Property.WeatherList.Weather[5].Date}}
          {% endif %}
      - name: yweather_min_6_precipitation
        unique_id: yweather_min_6_precipitation
        value_template:  >-
          {% if value_json.Feature[0].Property.WeatherList.Weather[6] %}
            {{value_json.Feature[0].Property.WeatherList.Weather[6].Rainfall}}
          {% endif %}
        device_class: precipitation
        unit_of_measurement: mm
      - name: yweather_min_6_date
        unique_id: yweather_min_6_date
        value_template:  >-
          {% if value_json.Feature[0].Property.WeatherList.Weather[6] %}
            {{value_json.Feature[0].Property.WeatherList.Weather[6].Date}}
          {% endif %}

sensors.yaml

sensors.yaml
- platform: template
  sensors:

# Yahoo Japan 気象情報API
# https://developer.yahoo.co.jp/webapi/map/openlocalplatform/v1/weather.html#:~:text=Yahoo!%E3%83%87%E3%83%99%E3%83%AD%E3%83%83
    yweather_min_0_precipitation_icon:
      unique_id: yweather_min_0_precipitation_icon
      friendly_name: Precipitation Level
      value_template: >-
        {{states('sensor.yweather_min_0_precipitation')}}
      icon_template: >-
        {% if states('sensor.yweather_min_0_precipitation') | float(0) <= 0 %}
          mdi:signal-cellular-outline
        {% elif states('sensor.yweather_min_0_precipitation') | float(0) < 3 %}
          mdi:signal-cellular-1
        {% elif states('sensor.yweather_min_0_precipitation') | float(0) < 10 %}
          mdi:signal-cellular-2
        {% elif states('sensor.yweather_min_0_precipitation') | float(0) < 20 %}
          mdi:signal-cellular-3
        {% elif states('sensor.yweather_min_0_precipitation') | float(0) < 50 %}
          mdi:alert
        {% else %}
          mdi:bomb
        {% endif %}
      device_class: precipitation
      unit_of_measurement: mm

    yweather_min_1_precipitation_icon:
      unique_id: yweather_min_1_precipitation_icon
      friendly_name: Precipitation Level 10min
      value_template: >-
        {{states('sensor.yweather_min_1_precipitation')}}
      icon_template: >-
        {% if states('sensor.yweather_min_1_precipitation') | float(0) <= 0 %}
          mdi:signal-cellular-outline
        {% elif states('sensor.yweather_min_1_precipitation') | float(0) < 3 %}
          mdi:signal-cellular-1
        {% elif states('sensor.yweather_min_1_precipitation') | float(0) < 10 %}
          mdi:signal-cellular-2
        {% elif states('sensor.yweather_min_1_precipitation') | float(0) < 20 %}
          mdi:signal-cellular-3
        {% elif states('sensor.yweather_min_1_precipitation') | float(0) < 50 %}
          mdi:alert
        {% else %}
          mdi:bomb
        {% endif %}
      device_class: precipitation
      unit_of_measurement: mm

    yweather_min_2_precipitation_icon:
      unique_id: yweather_min_2_precipitation_icon
      friendly_name: Precipitation Level 20min
      value_template: >-
        {{states('sensor.yweather_min_2_precipitation')}}
      icon_template: >-
        {% if states('sensor.yweather_min_2_precipitation') | float(0) <= 0 %}
          mdi:signal-cellular-outline
        {% elif states('sensor.yweather_min_2_precipitation') | float(0) < 3 %}
          mdi:signal-cellular-1
        {% elif states('sensor.yweather_min_2_precipitation') | float(0) < 10 %}
          mdi:signal-cellular-2
        {% elif states('sensor.yweather_min_2_precipitation') | float(0) < 20 %}
          mdi:signal-cellular-3
        {% elif states('sensor.yweather_min_2_precipitation') | float(0) < 50 %}
          mdi:alert
        {% else %}
          mdi:bomb
        {% endif %}
      device_class: precipitation
      unit_of_measurement: mm

    yweather_min_3_precipitation_icon:
      unique_id: yweather_min_3_precipitation_icon
      friendly_name: Precipitation Level 30min
      value_template: >-
        {{states('sensor.yweather_min_3_precipitation')}}
      icon_template: >-
        {% if states('sensor.yweather_min_3_precipitation') | float(0) <= 0 %}
          mdi:signal-cellular-outline
        {% elif states('sensor.yweather_min_3_precipitation') | float(0) < 3 %}
          mdi:signal-cellular-1
        {% elif states('sensor.yweather_min_3_precipitation') | float(0) < 10 %}
          mdi:signal-cellular-2
        {% elif states('sensor.yweather_min_3_precipitation') | float(0) < 20 %}
          mdi:signal-cellular-3
        {% elif states('sensor.yweather_min_3_precipitation') | float(0) < 50 %}
          mdi:alert
        {% else %}
          mdi:bomb
        {% endif %}
      device_class: precipitation
      unit_of_measurement: mm

    yweather_min_4_precipitation_icon:
      unique_id: yweather_min_4_precipitation_icon
      friendly_name: Precipitation Level 40min
      value_template: >-
        {{states('sensor.yweather_min_4_precipitation')}}
      icon_template: >-
        {% if states('sensor.yweather_min_4_precipitation') | float(0) <= 0 %}
          mdi:signal-cellular-outline
        {% elif states('sensor.yweather_min_4_precipitation') | float(0) < 3 %}
          mdi:signal-cellular-1
        {% elif states('sensor.yweather_min_4_precipitation') | float(0) < 10 %}
          mdi:signal-cellular-2
        {% elif states('sensor.yweather_min_4_precipitation') | float(0) < 20 %}
          mdi:signal-cellular-3
        {% elif states('sensor.yweather_min_4_precipitation') | float(0) < 50 %}
          mdi:alert
        {% else %}
          mdi:bomb
        {% endif %}
      device_class: precipitation
      unit_of_measurement: mm

    yweather_min_5_precipitation_icon:
      unique_id: yweather_min_5_precipitation_icon
      friendly_name: Precipitation Level 50min
      value_template: >-
        {{states('sensor.yweather_min_5_precipitation')}}
      icon_template: >-
        {% if states('sensor.yweather_min_5_precipitation') | float(0) <= 0 %}
          mdi:signal-cellular-outline
        {% elif states('sensor.yweather_min_5_precipitation') | float(0) < 3 %}
          mdi:signal-cellular-1
        {% elif states('sensor.yweather_min_5_precipitation') | float(0) < 10 %}
          mdi:signal-cellular-2
        {% elif states('sensor.yweather_min_5_precipitation') | float(0) < 20 %}
          mdi:signal-cellular-3
        {% elif states('sensor.yweather_min_5_precipitation') | float(0) < 50 %}
          mdi:alert
        {% else %}
          mdi:bomb
        {% endif %}
      device_class: precipitation
      unit_of_measurement: mm

    yweather_min_6_precipitation_icon:
      unique_id: yweather_min_6_precipitation_icon
      friendly_name: Precipitation Level 60min
      value_template: >-
        {{states('sensor.yweather_min_6_precipitation')}}
      icon_template: >-
        {% if states('sensor.yweather_min_6_precipitation') | float(0) <= 0 %}
          mdi:signal-cellular-outline
        {% elif states('sensor.yweather_min_6_precipitation') | float(0) < 3 %}
          mdi:signal-cellular-1
        {% elif states('sensor.yweather_min_6_precipitation') | float(0) < 10 %}
          mdi:signal-cellular-2
        {% elif states('sensor.yweather_min_6_precipitation') | float(0) < 20 %}
          mdi:signal-cellular-3
        {% elif states('sensor.yweather_min_6_precipitation') | float(0) < 50 %}
          mdi:alert
        {% else %}
          mdi:bomb
        {% endif %}
      device_class: precipitation
      unit_of_measurement: mm

表示例

sensor.yweather_min_N_precipitation_icon

type: custom:mushroom-chips-card
chips:
  - type: entity
    entity: sensor.yweather_min_0_precipitation_icon
    content_info: name
    icon_color: blue
    name::現在
  - type: entity
    entity: sensor.yweather_min_3_precipitation_icon
    content_info: name
    icon_color: blue
    name::+30分
  - type: entity
    entity: sensor.yweather_min_6_precipitation_icon
    icon_color: primary
    content_info: name
    name::+60分

Discussion