這篇是基於經常收不到政府地震示警的邊緣人提供的,
主要是教你如何連接上政府的示警api,
即時在手機上顯示警告通知。
政府的示警api來自這個網頁:民生示警公開平台
首先用home assistant連結這個平台的南投示警,
點進去後會產生json格式,
去下載postman來看內容
最底下的內容就是最新的示警訊息,
我們要做的就是擷取這段內容及時發佈。
然後有些前置作業要先做好,請參考Home assistant介面介紹
接著在packages建立natural_disaster.yaml
內容如下
sensor:
- platform: rest
name: disaster
json_attributes_path: "$..entry[-1:]"
json_attributes:
- updated #用來抓取更新時間
resource: https://alerts.ncdr.nat.gov.tw/JSONAtomFeed.ashx?County=%e5%8d%97%e6%8a%95%e7%b8%a3 #輸入postman網址會自動轉URL編碼,中文字會消失,原本是County=南投
value_template: "{{ value_json['entry'][-1]['summary']['#text'] }}" #用來抓取最新的示警內容
- platform: template
sensors:
disaster_time:
value_template: '{{ (as_timestamp(now()) - as_timestamp(states.sensor.disaster.attributes.updated)) / 600 }}' #用來確認示警時間在10分鐘以內發佈的內容才會在手機上示警,不然這個網頁一直更新內容手機會一直跳警示通知
這樣就會產生兩個實體如下
接著到設定的自動化設定示警
建立一個災害提醒
用我們建立的sensor.disater_time抓取10分鐘內更新的內容,
因為剛剛的設定數值value_template: '{{ (as_timestamp(now()) - as_timestamp(states.sensor.disaster.attributes.updated)) / 600 }}' 這個是現在的時間-示警的時間秒數/600秒,所以是以10分鐘為1單位,
自動化就以數值變動-1~1觸發,
10分鐘內的訊息會觸動。
避免太多無關的警戒通知
新增模版要求有南投的通知才會觸發
{{ '南投' in states.sensor.disaster.state }}
接著是觸發後的動作,手機要安裝home assistant app並登入,才能有觸發後的動作,service: notify.mobile_app_percy這個id是我的手機連接到home assistant的名稱。
service: notify.mobile_app_percy
data:
title: 災害警報
message: >-
{{ as_timestamp(states.sensor.disaster.attributes.updated) |
timestamp_custom('%b %d %X') }}{{ states('sensor.disaster') }}