Get rid of 'Couldn't connect to server' in log when httpResponses server is down

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
riko
Posts: 90
Joined: Saturday 22 August 2020 13:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Get rid of 'Couldn't connect to server' in log when httpResponses server is down

Post by riko »

I am using a script to monitor the production of a Omnik/Hosola solar inverter (see below if you would like to reuse this, it also include a warning when the production is too low and a daily report of production).

When the sun is not shining, the inverter is powered down. Which means that the http response will give an error in the domoticz log:

2023-01-17 19:11:03.558 Error: Error opening url: http://xx/js/status.js
2023-01-17 19:11:03.658 Error: dzVents: Error: (3.1.8) Omnik Solar: HTTP/1.1 response: 7 ==>> Couldn't connect to server

Is there a way to prevent that this error message is shown for this particular script? It pollutes my error log every night

Code: Select all

local MANUAL_OVERRIDE_SWITCH 	= 383
local SWITCH_OMVORMER			= 382

return {
	active = true,
	on = {
		timer = {
			'every minute',		-- starting from xx:00 triggers every xx minutes (0 > xx < 60)
		},
		httpResponses = { "Omnik" },  -- do not change this
		devices = { 
			MANUAL_OVERRIDE_SWITCH,
			SWITCH_OMVORMER
		},
	},
	logging = {
		level = domoticz.LOG_ERROR,
		marker = 'Omnik Solar',
	},
	data =     
    { 
        last_production = 
        { 
			initial = "0"  
		},	
	},
	
	execute = function(dz, item)

		local script_name			= 'Zonnepanelen 🌞: ' 
		
		-- uitschakelen van script
		local manualOverrideSwitch = dz.devices(MANUAL_OVERRIDE_SWITCH)
			
		if (item == manualOverrideSwitch and manualOverrideSwitch.state == 'On') then
			telegram.sendText(telegram.getId('rik'), script_name .. 'het script is handmatig uitgeschakeld (disable script switches)')
			dz.log(script_name ..  'is handmatig uitgeschakeld (disable script switches)', dz.LOG_DEBUG)
            return
		end	


        local maxResponseSeconds 	= 720 
        local OmnikIP 				= 'xx'   -- Your local Omnik inverter IP Address
        local production_device 	= 379              -- IDX of dummy electricity instant and counter device
        local total_device 			= 380              -- IDX of dummy custom sensor (total kwh)
		local current_device 		= 381
		local switch_omvormer		= dz.devices(SWITCH_OMVORMER)
		local alert 				= dz.devices(378)    		   -- Alert device

		local round = dz.utils.round


        local function checkWebpageResponse()
            local deltaSeconds = dz.time.dDate - dz.data.previousRainSample
            if ( maxResponseSeconds > 0 )  and ( deltaSeconds > maxResponseSeconds ) then
                dz.notify(scriptVar, 'Omnik Inverter did not respond within ' .. tostring(maxResponseSeconds) .. ' seconds.')
            end
        end		 
        local function getOmnikData()
           local urlstring = 'http://' .. OmnikIP .. '/js/status.js'
           dz.openURL  ({ url = urlstring, callback = "Omnik" })
        end	
        
        local function processHTM()
            OmnikData = (item.data):gsub('"',''):gsub("'","")
            OmnikArray = string.match(OmnikData, 'myDeviceArray%[0%].+,;')
            --dz.log('Omnik Array: ' .. OmnikArray,dz.LOG_INFO)
            splittedResult = dz.utils.stringSplit(OmnikArray,',')
            strOmnikCurrentWatt = splittedResult[2]                  -- Amount in Watt
            strOmnikkWhToday = splittedResult[3]                                -- Amount in kWh of the day, not used
            strOmnikTotalkWh = splittedResult[4]                               -- Total generated kWh needs to be converted to Wh
			OmnikCurrentWatt = tonumber(strOmnikCurrentWatt)
            OmnikkWhToday = tonumber(strOmnikkWhToday)/100
            OmnikTotalWh = tonumber(strOmnikTotalkWh)*100
            --dz.log("Received Data: Current Power: " .. OmnikCurrentWatt ..' W',dz.LOG_INFO)
            --dz.log("Received Data: Daily Power: " .. OmnikkWhToday .. ' kWh',dz.LOG_INFO)
            --dz.log("Received Data: Total Power: " .. OmnikTotalWh/1000 .. ' kWh',dz.LOG_INFO)
            -- update Omnik electricity instant and counter device 
            dz.devices(production_device).updateElectricity(OmnikCurrentWatt, OmnikTotalWh)
            dz.devices(total_device).updateCustomSensor(OmnikTotalWh/1000)
			dz.devices(current_device).updateCustomSensor(OmnikCurrentWatt)

         
        end
		 
		 -- Main
		
		if item.isTimer then
			--checkWebpageResponse()
			getOmnikData()
		elseif item.isHTTPResponse and item.ok then -- statusCode == 2xx
			processHTM(item.data)
			switch_omvormer.switchOn().checkFirst()
			--dz.log(item.data,dz.LOG_DEBUG)
		elseif item.isHTTPResponse then -- Inverter staat uit
			--telegram.sendText(telegram.getId('rik'), 'Zonnepanelen komt geen data binnen')
			--dz.log('Could not get (good) data from ' .. OmnikIP,dz.LOG_ERROR)
			--dz.log(item.data,dz.LOG_DEBUG)
			switch_omvormer.switchOff().checkFirst()
		elseif item == switch_omvormer then
			if switch_omvormer.state == 'Off' then
				telegram.sendText(telegram.getId('rik'), script_name .. 'Omvormer schakelt uit, dagproductie:' .. dz.devices(production_device).counterToday .. 'kWh')
			elseif switch_omvormer.state == 'On' then
				telegram.sendText(telegram.getId('rik'), script_name .. 'Omvormer schakelt in')
			end
		end


		if item.isTimer and dz.time.matchesRule('at 22:00') then
					
			local solar_production = dz.devices(production_device).counterToday   -- IDX van Productie zonnepanelen in W en totaal kWh
				
			if round(tonumber(solar_production),1) == 0 then		
				telegram.sendText(telegram.getId('rik'), '🔴 Zonnepanelen lijken niet te werken. Dagproductie: ' .. solar_production .. 'kWh')
				alert.updateAlertSensor(dz.ALERTLEVEL_RED, 'Zonnepanelen lijken niet meer te werken')
			elseif dz.data.last_production == solar_production then
				telegram.sendText(telegram.getId('rik'), '🔴 Zonnepanelen lijken niet te werken. Productie gelijk aan gisteren: ' .. solar_production .. 'kWh')
				alert.updateAlertSensor(dz.ALERTLEVEL_RED, 'Zonnepanelen lijken niet meer te werken')
			elseif round(tonumber(solar_production),1) >= 1 then	
				alert.updateAlertSensor(dz.ALERTLEVEL_GREEN, 'Zonnepanelen werken naar behoren')   
			--	telegram.sendText(telegram.getId('rik'), 'Zonnepanelen dagproductie:' .. solar_production .. 'kWh')
			elseif round(tonumber(solar_production),1) > 0 then	
				alert.updateAlertSensor(dz.ALERTLEVEL_ORANGE, 'Zonnepanelen lijken slechter te werken')
				telegram.sendText(telegram.getId('rik'), '🟡 Zonnepanelen lijken slechter te werken. Dagproductie: ' .. solar_production .. 'kWh')
			end
			
			dz.data.last_production = solar_production
			
		end
	end
}
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Get rid of 'Couldn't connect to server' in log when httpResponses server is down

Post by waltervl »

Change the timer from every minute to only during daylight or 1 hour before sunrise and one hour after sunset will save you a lot of error logging.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
riko
Posts: 90
Joined: Saturday 22 August 2020 13:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Get rid of 'Couldn't connect to server' in log when httpResponses server is down

Post by riko »

Hehe thanks for the out of the box thinking (don't know why i could not come with such a bright solution myself :))
mgugu
Posts: 208
Joined: Friday 04 November 2016 12:33
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: France
Contact:

Re: Get rid of 'Couldn't connect to server' in log when httpResponses server is down

Post by mgugu »

Less brilliant solution, you could ping the server before openUrl.
With linux OS it should be something like that:

Code: Select all

serversOK = os.execute('ping -c1 -w 1 '..IPaddr)
User avatar
boum
Posts: 130
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Get rid of 'Couldn't connect to server' in log when httpResponses server is down

Post by boum »

Well, maybe instead of doing the ping inside the script, you could use the System Alive hardware that will do the ping and you'll have a switch device you can test in your script.

The server might still react to the ping but not to the HTTP request. In this case maybe the HTTP poller can help, but I don't know how it works.
mgugu
Posts: 208
Joined: Friday 04 November 2016 12:33
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: France
Contact:

Re: Get rid of 'Couldn't connect to server' in log when httpResponses server is down

Post by mgugu »

boum wrote: Wednesday 18 January 2023 11:06 Well, maybe instead of doing the ping inside the script, you could use the System Alive hardware that will do the ping and you'll have a switch device you can test in your script.

The server might still react to the ping but not to the HTTP request. In this case maybe the HTTP poller can help, but I don't know how it works.
Domoticz is definitely a goldmine. I discover this utility today, thanks !
riko
Posts: 90
Joined: Saturday 22 August 2020 13:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Get rid of 'Couldn't connect to server' in log when httpResponses server is down

Post by riko »

Wow surprise here! I was doing online checks for other devices with Crojobs. But this is much more convenient!
Hcroij
Posts: 24
Joined: Sunday 27 November 2016 16:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Get rid of 'Couldn't connect to server' in log when httpResponses server is down

Post by Hcroij »

Hi all I got a similar problem.

using System aliver checker and it creates a "switch" so I can see if the system is alive.
the trouble is (hope you can see it in the small program).

"Sys Alive" creates an device with "IDX 1151" I gave it the name "Ctrl1".
If I use this in dzevents I get an error. The same error with any device (IDX or Name) create through "System alive checker"
Any other ( already existing ) IDX or name from a device it works.

Can anyone put me in the right direction? Imo it should be possible with IDX or Devicename?

p.s.
Domoticz
Compile Date: 2024-01-15 16:48:07
dzVents Version: 3.1.8

Code: Select all

return
{
        on =
    {
        timer   = {'every 1 minutes'}
    },
    execute = function(domoticz)
   — local itho = 884    -- excisting idx if used it WORKS no error
    — local lamp = 226   -- excisting idx of PC it WORKS no error
                            -- so this gives me an easy way to see if the script works
    local test = 1148   -- this is the IDX from the item created with "System alive Checker"
                        -- this one is causing the problem
    local test2 = 1151   -- Extra test ... router alive  Same problem              

	 if domoticz.devices(test2).state ~= 'Off' then
		domoticz.devices(lamp).switchOn()
	end    
end
}

Code: Select all

 2024-01-20 14:03:00.479 Error: dzVents: Error: (3.1.8) There is no device with that name or id: 1151
2024-01-20 14:03:00.479 Error: dzVents: Error: (3.1.8) An error occurred when calling event handler Script #1
2024-01-20 14:03:00.479 Error: dzVents: Error: (3.1.8) ...domoticz/scripts/dzVents/generated_scripts/Script #1.lua:18: attempt to index a nil value 
Last edited by Hcroij on Sunday 21 January 2024 12:35, edited 1 time in total.
User avatar
boum
Posts: 130
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Get rid of 'Couldn't connect to server' in log when httpResponses server is down

Post by boum »

Maybe newly created devices are not “yet” available in dzVents. It might be a bug. You can try to either restart domoticz or disable/reenable scripts/dzvents in Parameters.
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Get rid of 'Couldn't connect to server' in log when httpResponses server is down

Post by waltervl »

Dzvents does not like identical names for different devices. So the name should be unique.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Hcroij
Posts: 24
Joined: Sunday 27 November 2016 16:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Get rid of 'Couldn't connect to server' in log when httpResponses server is down

Post by Hcroij »

@Boum
Rebooted more then once
The script works perfect if I use an IDX that is not created by “System Alive”

In exact the same script Ireplace this IDX with an “System Alive” created IDX the error pops up.


@Walter
The items all have unique names and IDX’s
The “Local = xxxx” that you see in the script are test purpose to find out if the error was in the script.
They are not in the original script.
Last edited by Hcroij on Sunday 21 January 2024 12:53, edited 1 time in total.
Hcroij
Posts: 24
Joined: Sunday 27 November 2016 16:36
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Get rid of 'Couldn't connect to server' in log when httpResponses server is down

Post by Hcroij »

Looks like Isolved the problem although its more a workaround then a solution.

I want to see if my mediabox is “alive”
Systemalive created a device “Test-Mediabox”.
If you look at the properties of this device it is (automaticalle created) an “On/Off” switch.
As soon as I changed the properties to “Contact” the script runs fine.

Regardless is I use the “IDX” or the “devicename”
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests