WAN IP Checker Reinvented

Easy to use, 100% Lua-based event scripting framework.
Post Reply
User avatar
emme
Posts: 897
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

WAN IP Checker Reinvented

Post by emme » Monday 16 July 2018 10:50

Ciao,

based on the fact that 4.icfg.me seems to be no longer working and thans to dzVents new frameworks that support Http response,
I rewrite the wan IP checker that now looks much smarter:

Code: Select all

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

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

	execute = function(dz, devNil)
    local actIP = dz.devices('Wan IP').text 
    local newIP = 'Unable to retrieve IP'
    
        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 
        end
	end
}
..mmh... I don't know ho to update the wiki :P :P
The most dangerous phrase in any language is:
"We always done this way"

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests