Please help an idiot  [Solved]

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

Moderator: leecollings

Post Reply
theolsen
Posts: 46
Joined: Wednesday 31 July 2013 11:15
Target OS: -
Domoticz version:
Contact:

Please help an idiot

Post 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
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Please help an idiot  [Solved]

Post 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
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
theolsen
Posts: 46
Joined: Wednesday 31 July 2013 11:15
Target OS: -
Domoticz version:
Contact:

Re: Please help an idiot

Post by theolsen »

waaren, you are a god amongst men.

Worked perfectly. Thank you so much.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest