Page 1 of 1

If you need weather from any server if answer is JSON

Posted: Saturday 01 July 2023 19:36
by habahabahaba
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 :

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
}
5. thats all.

Re: If you need weather from any server if answer is JSON

Posted: Wednesday 19 July 2023 15:52
by habahabahaba
FORECAST is not a problem too.

api.openweathermap.org/data/2.5/forecast?&lang=us&lat=57.6299&lon=39.8737&appid=<YOUR_API_KEY> - the link

Code: Select all

return {
	on = {
		timer = {'at 06:58 on mon,tue,wed,thu,fri',		
		         'at 07:55 on sat',
		         'at 08:55 on sun'
		        },
		
		httpResponses = {'weatherForecastOpenWeather'}	
	},


	execute = function(domoticz, item)
		
		if (item.isTimer) then
			domoticz.openURL({
				url = 'https://api.openweathermap.org/data/2.5/forecast?&lang=us&lat=57.6299&lon=39.8737&appid=<YOU_API_KEY>',
				method = 'GET',
				callback = 'weatherForecastOpenWeather', -- see httpResponses above.
			}).afterSec(5)
        end
    
	    if (item.isHTTPResponse) then
	        
	        if (item.ok) then
	            
	            if (item.isJSON) then
	              
	                local jsonParser = require('JSON')
				    local json1 = jsonParser:decode(item.data)
				   				   
				    local dateTime1 = os.date("%H:%M",json1['list'][1]['dt'])
				    local dateTime2 = os.date("%H:%M",json1['list'][2]['dt'])
				    local dateTime3 = os.date("%H:%M",json1['list'][3]['dt'])
				    local dateTime4 = os.date("%H:%M",json1['list'][4]['dt'])
				    
				    
				    local temp1 = (json1['list'][1]['main']['temp']-273)
				    local temp2 = (json1['list'][2]['main']['temp']-273)
				    local temp3 = (json1['list'][3]['main']['temp']-273)
				    local temp4 = (json1['list'][4]['main']['temp']-273)
				    
				    
				    local pressure1 = (json1['list'][1]['main']['pressure']*0.750064)
				    local pressure2 = (json1['list'][2]['main']['pressure']*0.750064)
				    local pressure3 = (json1['list'][3]['main']['pressure']*0.750064)
				    local pressure4 = (json1['list'][4]['main']['pressure']*0.750064)
				    
				    
				    local humidity1 = (json1['list'][1]['main']['humidity'])
				    local humidity2 = (json1['list'][2]['main']['humidity'])
				    local humidity3 = (json1['list'][3]['main']['humidity'])
				    local humidity4 = (json1['list'][4]['main']['humidity'])
				    
				    
				    local description1 = (json1['list'][1]['weather'][1]['description'])
				    local description2 = (json1['list'][2]['weather'][1]['description'])
				    local description3 = (json1['list'][3]['weather'][1]['description'])
				    local description4 = (json1['list'][4]['weather'][1]['description'])
				     
				     
				    local windSpeed1 = (json1['list'][1]['wind']['speed'])
				    local windSpeed2 = (json1['list'][2]['wind']['speed'])
				    local windSpeed3 = (json1['list'][3]['wind']['speed'])
				    local windSpeed4 = (json1['list'][4]['wind']['speed'])
				    
				    
				    local windGust1 = (json1['list'][1]['wind']['gust'])
				    local windGust2 = (json1['list'][2]['wind']['gust'])
				    local windGust3 = (json1['list'][3]['wind']['gust'])
				    local windGust4 = (json1['list'][4]['wind']['gust'])
				     
				    
                        
                    local str1 = dateTime1..': Temp.: '..math.floor(temp1+0.5)..'°С , press.: '..math.floor(pressure1+0.5)..'mm, hum.: '..humidity1..'%, wind: '..math.floor(windSpeed1+0.5)..'-'..math.floor(windGust1+0.5)..'m/s, '..description1
                    local str2 = dateTime2..': Temp.: '..math.floor(temp2+0.5)..'°С , press.: '..math.floor(pressure1+0.5)..'mm, hum.: '..humidity2..'%, wind: '..math.floor(windSpeed2+0.5)..'-'..math.floor(windGust2+0.5)..'m/s, '..description2
                    local str3 = dateTime3..': Temp.: '..math.floor(temp3+0.5)..'°С , press.: '..math.floor(pressure1+0.5)..'mm, hum.: '..humidity3..'%, wind: '..math.floor(windSpeed3+0.5)..'-'..math.floor(windGust3+0.5)..'m/s, '..description3
                    local str4 = dateTime4..': Temp.: '..math.floor(temp4+0.5)..'°С , press.: '..math.floor(pressure1+0.5)..'mm, hum.: '..humidity4..'%, wind: '..math.floor(windSpeed4+0.5)..'-'..math.floor(windGust4+0.5)..'m/s, '..description4
                    
                    local commStr = 'Weather forecast for nearest 12 hours:'..'\n'..str1..'\n'..str2..'\n'..str3..'\n'..str4
                    
 	                domoticz.executeShellCommand('curl -v -X POST --silent --output /dev/null https://api.telegram.org/<you bot number>/sendMessage -d chat_id=<your chat id> -d text="'..commStr..'"') -- sending notification to telegram
	                
	           end
	            
	       end
	        
	   end

Re: If you need weather from any server if answer is JSON

Posted: Wednesday 19 July 2023 17:48
by kiddigital
Thats a nice way as well :)

You are aware there is a native OpenWeatherMap hardware module build-in? No need, unless you want to do other things than the standard, to use dzVents for standard weather.

Re: If you need weather from any server if answer is JSON

Posted: Thursday 20 July 2023 16:12
by Kedi
You better put "&units=metric" in the url, because then you do not have to convert from Kelvin to Celsius