前言
用過一段時間的彩雲天氣 APP,最吸引我的地方是精確到局部區域的天氣預測,雖然准確度並不算高,但是對於預測下雨還是不錯的選擇。在 Domoticz 中添加彩雲天氣的數據,利用的是彩雲天氣提供的 API,本文參考了 Domoticz 官方文檔 和 http/https poller 的使用,在此表示感謝。
步驟
在設置 → 硬件中添加一項 HTTP/HTTPS poller,填入 URL,此處需要加入自己的經緯度,點此處查詢,URL 中的 API_KEY 來源於 github
https://api.caiyunapp.com/v2/Y2FpeXVuIGFuZHJpb2QgYXBp/116.404412,39.915156/realtime.json
點擊“創建虛擬傳感器”,依次添加溫度、濕度、氣壓、PM2.5、PM10,其中 PM2.5、PM10 類型為 Custom Sensor,單位 ug/m³。添加完成后在設置 → 設備中可看到各項添加的傳感器
在樹莓派的 /home/pi/domoticz/scripts/lua_parsers 目錄添加 caiyun_paraser.lua 文件,內容如下,結尾的 domoticz_updateDevice 第一個參數要修改為上圖中對應的 Idx
s = request['content'];
local temperature = domoticz_applyJsonPath(s, '.result.temperature')
local humidity = domoticz_applyJsonPath(s, '.result.humidity')
local hum_stat = '0'
local bar = domoticz_applyJsonPath(s, '.result.pres')
local bar_for = '0'
local skycon = domoticz_applyJsonPath(s, '.result.skycon')
local pm25 = domoticz_applyJsonPath(s, '.result.pm25')
local pm10 = domoticz_applyJsonPath(s, '.result.pm10')
if humidity >= 0.4 and humidity <= 0.6 then
hum_stat = '1'
elseif humidity >= 0.3 and humidity <= 0.8 then
hum_stat = '0'
elseif humidity > 0.8 then
hum_stat = '3'
elseif humidity < 0.3 then
hum_stat = '2'
end
if skycon == 'CLEAR_DAY' or skycon == 'CLEAR_NIGHT' then
bar_for = '1'
elseif skycon == 'PARTLY_CLOUDY_DAY' or skycon == 'PARTLY_CLOUDY_NIGHT' then
bar_for = '2'
elseif skycon == 'CLOUDY' then
bar_for = '3'
elseif skycon == 'RAIN' then
bar_for = '4'
end
domoticz_updateDevice(3, 0, temperature)
domoticz_updateDevice(4, humidity*100, hum_stat)
domoticz_updateDevice(5, 0, tostring(bar/100)..';'..bar_for)
domoticz_updateDevice(6, 0, pm25)
domoticz_updateDevice(7, 0, pm10)
最終效果圖