If you need weather from any server if answer is JSON
Posted: Saturday 01 July 2023 19:36
Hi everybody.
Here is an example how to get weather conditions from any sourse (ex. is of OpenWeather) if answer is in JSON.
1. Make sure dzVents is enabled
2. Add dummy hardware
3. Add virtual temp, humy, pressure sensors
4. add new script :
5. thats all.
Here is an example how to get weather conditions from any sourse (ex. is of OpenWeather) if answer is in JSON.
1. Make sure dzVents is enabled
2. Add dummy hardware
3. Add virtual temp, humy, pressure sensors
4. add new script :
Code: Select all
return {
on = {
timer = {
'every 6 minutes' -- just an example set as often as you need
},
httpResponses = {
'OpenWeathertrigger' -- must match with the callback passed to the openURL command
}
},
logging = {
level = domoticz.LOG_INFO,
marker = 'template',
},
execute = function(domoticz, item)
if (item.isTimer) then
domoticz.openURL({
url = 'https://api.openweathermap.org/data/2.5/weather?&lang=us&lat=57.6299&lon=39.8737&appid=<YOU_API_KEY>',
method = 'GET',
callback = 'OpenWeathertrigger', -- see httpResponses above.
}).afterSec(5)
end
if (item.isHTTPResponse) then
if (item.ok) then
if (item.isJSON) then
local jsonParser = require('JSON') -- seems to be the file /Domoticz/scripts/lua/JSON.lua - JSON library
local json1 = jsonParser:decode(item.data)
local humSensor = domoticz.devices(8) --virtual humidity sensor of Dummy hardware with ID 8
----- setting up comfort level
-- 3 - wet, 1 - comfort, 0 - normal, 2 - dry
local humFeel = json1['main']['humidity']
local humsensorFeel = 0
if humFeel >= 72 then
humsensorFeel = 3
elseif humFeel <= 40 then
humsensorFeel = 2
elseif (humFeel > 40 and humFeel < 60 ) then
humsensorFeel = 1
end
----- end setting ----------
local temp1 = math.floor((json1['main']['temp']-273)+0.5) -- From Kelvin decimal to a Celsius whole
domoticz.devices(5).updateTemperature(temp1) -- 5 - is ID of virtual temp sensor
domoticz.devices(6).updatePressure(json1['main']['pressure']*0.750064) -- pressure from Pa to mm Hg -- 6 is an ID of virtual pressure sensor
humSensor.update(json1['main']['humidity'], humsensorFeel)
end
else
domoticz.log('there is an error of getting data from OpenWeather', domoticz.LOG_ERROR)
domoticz.log(item, domoticz.LOG_ERROR)
end
end
end
}