DzVents脚本获取中国天气网实时室外天气温湿度数据

文档:
https://en.domoticz.cn/wiki/DzVents:_ne ... _scripting
回复
头像
DT27
帖子: 345
注册时间: 周四 3月 30, 2017 08:54
Gender:

DzVents脚本获取中国天气网实时室外天气温湿度数据

帖子 DT27 »

截屏2023-12-21 12.44.00.png
截屏2023-12-21 12.44.00.png (45.32 KiB) 查看 19145 次
DzVents脚本:

代码: 全选

return {
    on = {
        --每5分钟更新一次
        timer = { 'every 5 minutes' },
        httpResponses = {
            'd_response'
        }
    },
    execute = function(domoticz, item)
        --天气地址码,根据自己的地区更改,从链接中获取:http://www.weather.com.cn/weather1d/101010100.shtml
        local location = 101120509
        --温湿度设备名称
        local deviceName = '室外'
        
        if (item.isTimer) then
            domoticz.openURL({
                headers = {['Host']='d1.weather.com.cn',['Referer']='http://www.weather.com.cn/',['User-Agent']='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36' },
                url = 'http://d1.weather.com.cn/sk_2d/'..location..'.html',
                method = 'GET',
                callback = 'd_response',
            })
        end

        if (item.isHTTPResponse and item.ok) then
            data_s = string.match(item.data,'({.*})')
            local d_json = domoticz.utils.fromJSON(data_s)
            domoticz.devices(deviceName).updateTempHum(d_json["temp"],d_json["SD"]:sub(1, -2))
        end
    end
}
回复