Trying to get URL data and load it to a text sensor

Moderator: leecollings

Post Reply
svanni
Posts: 7
Joined: Wednesday 16 January 2019 6:35
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10693
Contact:

Trying to get URL data and load it to a text sensor

Post by svanni »

My goal is to update a text sensor with my most recent Public IP Address.
I am currently using the RPi's CRON to email myself the info...

4 * * * * wget http://api.ipify.org -O latest_ip.txt
5 * * * * mpack -s "Latest IP Address" /home/pi/latest_ip.txt [email protected]

However, I'd rather do this using a Lua script in Domoticz, and load the data into a text sensor.
Any suggestions ?

Thanks in advance.
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Trying to get URL data and load it to a text sensor

Post by emme »

there is a much easier way to obtain in within Domoticz using a dzVents Script.
I use a script with a Http request ans response (async mode) with the plus of updating my IPv6 Tunnel from HE:
here is it...

Code: Select all

local getIp    = 'https://api.ipify.org/?format=json'
local ipv6UPD  = 'https://<userID>:<token>@ipv4.tunnelbroker.net/nic/update?hostname=<TunnelID>'

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 = 'Impossibile recuperare IP'
    
        if (devNil.isTimer) or (devNil.isDevice) then
            dz.log("Invio Richiesta WanIP")
            dz.openURL({
                url = getIp,
                method = 'GET',
                callback = 'wanIP'
                })
        elseif (devNil.isHTTPResponse) then
            dz.log("Ritorno Richiesta WanIP")
            if (devNil.ok) then -- statusCode == 2xx
                newIP = devNil.json.ip
            end 
            if newIP ~= actIP then 
                dz.openURL({
                    url = ipv6UPD ,
                    method = 'GET',
                    callback = 'updIPv6Tunnel'
                })
                dz.devices('Wan IP').updateText(newIP)
            end 
        end
	end
}
The most dangerous phrase in any language is:
"We always done this way"
svanni
Posts: 7
Joined: Wednesday 16 January 2019 6:35
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10693
Contact:

Re: Trying to get URL data and load it to a text sensor

Post by svanni »

Thanks emme, I'll give that a try later this week.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest