3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Hass.io Yahoo 気象情報APIセンサー

Last updated at Posted at 2019-02-20

Hass.io カスタムコンポーネントを作ってみるでは「Hass.io」で WEB API をウォッチする センサーをの作り方を調べてみました。

今回は実際に気象情報API降水強度実測値降水強度予測値を取得するセンサーを作ってみましょう。このセンサーで、雨降り前に Google Home に「10分後に雨が降ります」などと喋らせること Automation が作成できますね。

ソースコード

実際のソースコード
↑このコードを
/config/custom_components/sensor/precipitation.py
として保存して再起動するとセンサーと稼働します

precipitation.PNG

使い方と設定

ドキュメント Hass.io Yahoo 気象情報APIセンサー をご参照ください


ヒント

乱暴にAPIアクセス部分を切り出せばこんな感じか

sample.py
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import json
...

url = 'https://map.yahooapis.jp/weather/V1/place'
params = {
  'coordinates':[LAT_AND_LON],
  'output': 'json',
  'appid': [YOUR APPID],
}

websession = async_get_clientsession(self.hass)
res = await websession.get(url, params=urlparams)

jsonText = await res.text()
jsonDict = json.loads(jsonText)
rainfall = jsonDict['Feature'][0]['Property']['WeatherList']['Weather'][0]['Rainfall']
参考:気象情報APIの返り値 (JSON)

これをパースして利用するわけですね

place.json
{  
   "ResultInfo":{  
      "Count":1,
      "Total":1,
      "Start":1,
      "Status":200,
      "Latency":0.004484,
      "Description":"",
      "Copyright":"(C) Yahoo Japan Corporation."
   },
   "Feature":[  
      {  
         "Id":"[ID]",
         "Name":"地点([LAT_AND_LON])の2019年02月16日 01時55分から60分間の天気情報",
         "Geometry":{  
            "Type":"point",
            "Coordinates":"[LAT_AND_LON]"
         },
         "Property":{  
            "WeatherAreaCode":6200,
            "WeatherList":{  
               "Weather":[  
                  {  
                     "Type":"observation",
                     "Date":"201902160155",
                     "Rainfall":0.00
                  },
                  {  
                     "Type":"forecast",
                     "Date":"201902160205",
                     "Rainfall":0.00
                  },
                  {  
                     "Type":"forecast",
                     "Date":"201902160215",
                     "Rainfall":0.00
                  },
                  {  
                     "Type":"forecast",
                     "Date":"201902160225",
                     "Rainfall":0.00
                  },
                  {  
                     "Type":"forecast",
                     "Date":"201902160235",
                     "Rainfall":0.00
                  },
                  {  
                     "Type":"forecast",
                     "Date":"201902160245",
                     "Rainfall":0.00
                  },
                  {  
                     "Type":"forecast",
                     "Date":"201902160255",
                     "Rainfall":0.00
                  }
               ]
            }
         }
      }
   ]
}
3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?