Page 1 of 1

Please help an idiot

Posted: Thursday 07 January 2021 14:12
by theolsen
I have plagiarised some script from here to get my WAN IP address and write it to a dummy text sensor.
That part works perfectly of course.

What I am trying to do is if that IP address does not match the address that I am expecting then turn on a light as a way of warning me.

Code I have messed up is below.

Basically, the light never comes on.
Can anyone tell me where I am going wrong?

Thanks in advance.


Code: Select all

local getIp    = 'https://api.ipify.org/?format=json'



return {
	on = {
		timer =         { 'every minute' },
		httpResponses = { 'wanIP' }
	},
	
	logging = {
        level = domoticz.LOG_FORCE,
        marker = '[WAN IP]'
    },

	execute = function(dz, devNil)
    local actIP = dz.devices('Wan IP').text 
    
  
    

      if (devNil.isTimer)  then
            
            dz.log("Send WanIP request")
            dz.openURL({
                url = getIp,
                method = 'GET',
                callback = 'wanIP'
                })
        elseif (devNil.isHTTPResponse) then
            dz.log("WanIP response")
            if (devNil.ok) then -- statusCode == 2xx
                newIP = devNil.json.ip
              
            end 
            
        
              
            if newIP ~= actIP then 
                dz.devices('Wan IP').updateText(newIP)
                
             
              
              
             end
                if newIP ~= '195.150.251.189' and newIP ~= 'nil' then
                    
                     execute = function(domoticz, doit)  
                local light = domoticz.devices('Hallway')
              light.switchOn()
        
end
        end
	end
end
}

Re: Please help an idiot  [Solved]

Posted: Thursday 07 January 2021 15:05
by waaren
theolsen wrote: Thursday 07 January 2021 14:12 What I am trying to do is if that IP address does not match the address that I am expecting then turn on a light as a way of warning me.
Can anyone tell me where I am going wrong?
Hard to tell without the log.

This should do it and if not please show the loglines

Code: Select all

local scriptVar = 'wanIP'
local getIp = 'https://api.ipify.org/?format=json'

return
{
    on =
    {
        timer =
        {
            'every minute',
        },
        httpResponses =
        {
            scriptVar,
        },
    },

    logging = {
        level = domoticz.LOG_FORCE,
        marker = '[WAN IP]',
    },

    execute = function(dz, item)
        local actIP = dz.devices('Wan IP')
        local hallWay = dz.devices('Hallway')
        local defaultIP = '195.150.251.189'

        if item.isTimer then
            dz.log("Send WanIP request")
            dz.openURL(
            {
                url = getIp,
                callback = scriptVar,
            })
        elseif item.isHTTPResponse and item.json then
            dz.log("WanIP response")
            local newIP = item.json.ip

            if newIP and newIP ~= actIP.text then
                actIP.updateText(newIP)
            end
            if newIP and newIP ~= defaultIP then
                hallWay.switchOn()
            end
        else
            dz.log('Problem with HTTP response', dz.LOG_ERROR)
            dz.log(item, dz.LOG_DEBUG)
        end
    end
}


Re: Please help an idiot

Posted: Thursday 07 January 2021 15:09
by theolsen
waaren, you are a god amongst men.

Worked perfectly. Thank you so much.