Python Plugin: Wan IP Checker

Python and python framework

Moderator: leecollings

manjh
Posts: 708
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: Python Plugin: Wan IP Checker

Post by manjh »

manjh wrote: Sunday 11 February 2018 16:51 I'm confused. I have this plugin running on my "production" Domoticz server with no problems.
But now I am setting up a second R-Pi as testbed, and somehow can't get the WAN IP checker installed.
Created the WAN-IP-CHECKER directory with the plugin.py file in it.
Then restarted Domoticz service, but the hardware dropdown does not show the plugin.
I double checked and found nothing wrong. Also tried a full reboot, no luck.

Any idea what might be causing this? I'm sure it must be something I forgot, but what? :o :shock:
Apologies, I just KNEW it had to be something stupid. Just found out I forgot to install python3.... everything OK now!

Might be useful to add a line to the instructions?

Code: Select all

Make sure you have Python3 installed! 
sudo apt-get install python3-dev
Hans
manjh
Posts: 708
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: Python Plugin: Wan IP Checker

Post by manjh »

One more for the wish-list: allow the user to change the subject of the warning mail.
Especially when a user runs more than one Domoticz system, it is very useful if the subject describes the system.
All it would take is an inputfield on the HW page for the plugin.
Hans
User avatar
ycahome
Posts: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

Re: Python Plugin: Wan IP Checker

Post by ycahome »

manjh wrote: Sunday 11 February 2018 17:09
Thank you for your suggestions, will be considered at next release!!!
manjh
Posts: 708
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: Python Plugin: Wan IP Checker

Post by manjh »

I am having some trouble keeping my Domoticz server stabile. Tried everything, eventually removed python3 and it seems to have effect. Weird.
Of course this means that the WAN IP Checker no longer works, since it needs python3.

Questions:
1. does this sound familiar at all?
2. would it be possible to make WAN IP Checker work without python3-dev?
Hans
User avatar
ycahome
Posts: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

Re: Python Plugin: Wan IP Checker

Post by ycahome »

Last time I recall to have this problem I ended up to rebuild my Domoticz (not a happy period)!! :( :(

As for the python, I suppose that can be done also with LUA.
manjh
Posts: 708
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: Python Plugin: Wan IP Checker

Post by manjh »

Due to the problems after installing the python3-dev3 lib, I decided to go back to the drawing board and start over.
I pulled out the latest dzVents version of Emme, played around with it a little and found that it works good enough for me.
Agreed, it does not have the nice features of the plugin. But I am OK with this version.

One item that I did not solve: detect if the text device exists, and create it if not. Not a big deal, since it should only occur immediately after the first install. Once the device is there it is OK.

I will post the code below. Please note that this is in no way an attempt to make a better version of the plugin: my only purpose was to get things working without the python3-dev3 lib. And that I achieved.

Installation is very simple: create the text device, then place the code in a file in the right directory. That's it, provided the notification has been setup in Domoticz.

Code:

Code: Select all

-- Based upon a script written by Emme (Milano, Italy)
-- Slightly modified and translated by Manjh
--
-- Store the script as GetWANIP.lua in domoticz/scripts/dzVents/scripts
-- 
-- Script will:
-- 1. retrieve current WN IP address
-- 2. compare to previous IP
-- 3. send warning msg if different
--
-- runs every four hours, but interval can be changed
--
-- check statements marked "*****", modify if needed
--
-- create a Domoticz text device:
-- Settings-->Hardware-->virtual-->create virtual sensor
-- use "Actual WAN IP" as name, create type "text"
--

return {
	on = {
	     timer = { 'every 4 hours' } -- ***** interval
	},
	
	logging = {
            level = domoticz.LOG_FORCE,
            marker = '[WAN IP]'
    },

    execute = function(dz, devNil)
        local dzb      = dz.LOG_FORCE
        local getIp    = 'curl -s https://4.ifcfg.me/' -- URL of website to echo my WAN IP
        local tmpFile  = '/home/pi/domoticz/scripts/wanip.txt' -- location where to store the temp file
        local actIp    = ''

        local devIp    = dz.devices('Actual WAN IP') -- ***** Name of Domoticz text device
	local currIp   = devIp.text
	local location = 'Utrecht' -- ***** your location: used in subject of warning msg
	
	-- get current WAN IP and store in a temp file
        os.execute(getIp..' > '..tmpFile)
        actIp = io.open(tmpFile):read()
        
        if actIp == nil then actIp = 'Cannot retrieve WAN IP' end 
        
        if actIp ~= currIp then
            msgTxt = 'WAN IP has changed: '..currIp..' ==> '..actIp
            dz.log(msgTxt, dzb)
            dz.notify('WAN IP change warning '..location, msgTxt, dz.PRIORITY_EMERGENCY)
            devIp.updateText(actIp)
            os.execute('rm '..tmpFile)
        else 
            dz.log('No change in WAN IP', dzb)
        end 
    end
}
Hans
User avatar
ycahome
Posts: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

Re: Python Plugin: Wan IP Checker

Post by ycahome »

As soon as you mention "Emme" that is the original author of the first LUA script, no problem at all.
manjh
Posts: 708
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: Python Plugin: Wan IP Checker

Post by manjh »

I created a page in the Wiki, but can't really get the code formatting correct.
Hans
User avatar
ycahome
Posts: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

Re: Python Plugin: Wan IP Checker

Post by ycahome »

manjh wrote: Friday 02 March 2018 9:48 I created a page in the Wiki, but can't really get the code formatting correct.
Dear manjh don't forget that this is the python thread.
Your script should go to LUA thread in order to be found by new users. ;)
User avatar
lmet
Posts: 23
Joined: Wednesday 19 July 2017 22:35
Target OS: Raspberry Pi / ODroid
Domoticz version: v4.9700
Location: France
Contact:

Re: Python Plugin: Wan IP Checker

Post by lmet »

Hi

Your program works perfectly thank you.

One suggestion: To be able to set who should receive notifications, currently everyone is notified (email, sms) so my wife receives a notification. She asks me: what is a WAN-IP address :)
User avatar
ycahome
Posts: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

Re: Python Plugin: Wan IP Checker

Post by ycahome »

:lol: :lol: :lol:

This is how domoticz works. You are notified ,as such,for every event,not only for wan ip.

Am planning to change notifications, to email only, on the next version but still only the preconfigured recipient will receive notifications.

In order to have different recipients for each plugin, smtp settings should be exported to plugin framework.
User avatar
Dnpwwo
Posts: 819
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Python Plugin: Wan IP Checker

Post by Dnpwwo »

@ycahome,

I'm not sure that is absolutely true.

Domoticz works by allowing users to configure their own notifications for devices using the device 'Edit' button in the Web UI to meet their needs. The Plugin Framework does not have capabilities for creating device notifications for that reason.

When the IP address changes, can you not change the device state so that it goes from 1 to 0 for a heartbeat then back to 1? That would allow users to choose what notification(s) they want in the normal way.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
User avatar
ycahome
Posts: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

Re: Python Plugin: Wan IP Checker

Post by ycahome »

Dnpwwo wrote: Monday 05 March 2018 5:47 @ycahome,

I'm not sure that is absolutely true.
@Dnpww0,

when I say "this is how Domoticz works" I mean the notification capabilities via python. Agree with you on that but, I believe that Domoticz smtp settings should be exported in order to be available via python framework ( Domoticz.smtpserver() , Domoticz.smtpport() etc. ).

As for your suggestion, the answer is yes, we can do it. But I don't know if "text" devices have such option.
A text device is required in order to record the new IP. I don't want to create a second device just for notifications.
A good option would be to have onchange() notification on "Text" devices.
lmet wrote: Sunday 04 March 2018 18:18 currently everyone is notified (email, sms) so my wife receives a notification. She asks me: what is a WAN-IP address :)
@lmet,

Corrected. Now only email is used for notification.
User avatar
lmet
Posts: 23
Joined: Wednesday 19 July 2017 22:35
Target OS: Raspberry Pi / ODroid
Domoticz version: v4.9700
Location: France
Contact:

Re: Python Plugin: Wan IP Checker

Post by lmet »

Hi,

Thanks
May be a workaround using a dummy type "Alert" there is place to put a text (Wan IP) and a "Notification" tab enabling the user to have it's own notification(s).
User avatar
ycahome
Posts: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

Re: Python Plugin: Wan IP Checker

Post by ycahome »

ok modified from Text to Alert.

Can you check and tell me if it is ok now? Unfortunately you should delete old device in order new one to be created!!
User avatar
lmet
Posts: 23
Joined: Wednesday 19 July 2017 22:35
Target OS: Raspberry Pi / ODroid
Domoticz version: v4.9700
Location: France
Contact:

Re: Python Plugin: Wan IP Checker

Post by lmet »

mea culpa :(
Alert needs a condition (greater,less, equal ...) an a value before sending notification.
May be someone else will find a workaround. Keep in mind that your code was Ok
Reversing to previous version. Sorry for wasting your time.
User avatar
ycahome
Posts: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

Re: Python Plugin: Wan IP Checker

Post by ycahome »

lmet wrote: Tuesday 06 March 2018 16:55 mea culpa :(
Alert needs a condition (greater,less, equal ...) an a value before sending notification.
May be someone else will find a workaround. Keep in mind that your code was Ok
Reversing to previous version. Sorry for wasting your time.
Don't mind about this change, was necessary anyway. Old functionality still works .

As for the notification, Device State is changed to "2" (amber) upon every change of IP and reverted back to "1" (normal) at next heartbeat (thanks Dnpwwo for this idea). Am trying to monitor via "OnChange" or "Usage" values.
Nickkkk72
Posts: 8
Joined: Tuesday 11 July 2017 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8805-b
Location: France
Contact:

Re: Python Plugin: Wan IP Checker

Post by Nickkkk72 »

@ycahome , hi, i've got a log error with your plugin :
Error: (Wanip) URL Request error: URL: https://4.ifcfg.me/ip
Since many weeks, i try to find a "usable" domoticz... (cf pyhon plugin system errors since end of 2017 in beta versions)
i use beta v3.9014, using python 3.4.2 (and 2.7 installed), on raspberry pi3, and actually, my distri is Jessie (roll back cause impossible to do anything by my side with Stretch). python3 and python3-dev are installed and checked. python3 -V and python -V show twice python 3.4.2
I can add WAN IP Checker hardware, test manually the URL throw browser, and the Wan IP control is well created, but nothing else... just the URL request error in Domoticz log.

Any idea ?
Nickkkk72
Posts: 8
Joined: Tuesday 11 July 2017 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8805-b
Location: France
Contact:

Re: Python Plugin: Wan IP Checker

Post by Nickkkk72 »

And my logs in debug mode :
2018-03-11 12:12:29.951 (Wanip) Started.
2018-03-11 12:12:30.269 (Wanip) Entering work loop.
2018-03-11 12:12:30.269 (Wanip) Initialized version 1.2.5, author 'ycahome'
2018-03-11 12:12:30.322 (Wanip) Debug log level set to: 'true'.
2018-03-11 12:12:30.322 (Wanip) 'Version':'1.2.5'
2018-03-11 12:12:30.322 (Wanip) 'DomoticzBuildTime':'2018-03-10 14:16:03'
2018-03-11 12:12:30.322 (Wanip) 'HomeFolder':'/home/pi/domoticz/plugins/Speedtest/'
2018-03-11 12:12:30.322 (Wanip) 'Mode6':'Debug'
2018-03-11 12:12:30.322 (Wanip) 'Mode1':'60'
2018-03-11 12:12:30.322 (Wanip) 'DomoticzHash':'6b5dd0b7'
2018-03-11 12:12:30.322 (Wanip) 'Port':'0'
2018-03-11 12:12:30.322 (Wanip) 'Key':'WAN-IP-CHECKER'
2018-03-11 12:12:30.322 (Wanip) 'HardwareID':'3'
2018-03-11 12:12:30.322 (Wanip) 'Name':'Wanip'
2018-03-11 12:12:30.322 (Wanip) 'Mode3':'Disable'
2018-03-11 12:12:30.322 (Wanip) 'DomoticzVersion':'3.9014'
2018-03-11 12:12:30.322 (Wanip) 'Author':'ycahome'
2018-03-11 12:12:30.322 (Wanip) 'Address':'http://4.ifcfg.me/ip'
2018-03-11 12:12:30.322 (Wanip) Device count: 1
2018-03-11 12:12:30.322 (Wanip) Device: 1 - ID: 5, Name: 'Wanip - WAN IP Alert', nValue: 0, sValue: 'No Alert!'
2018-03-11 12:12:30.322 (Wanip) Device ID: '5'
2018-03-11 12:12:30.322 (Wanip) Device Name: 'Wanip - WAN IP Alert'
2018-03-11 12:12:30.322 (Wanip) Device nValue: 0
2018-03-11 12:12:30.322 (Wanip) Device sValue: 'No Alert!'
2018-03-11 12:12:30.322 (Wanip) Heartbeat interval set to: 60.
2018-03-11 12:12:39.790 (Wanip) Calling message handler 'onHeartbeat'.
2018-03-11 12:12:39.790 (Wanip) onHeartbeat called
2018-03-11 12:12:39.840 Error: (Wanip) URL Request error: URL: http://4.ifcfg.me/ip
PS : What is 'Port':'0' ?
User avatar
ycahome
Posts: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

Re: Python Plugin: Wan IP Checker

Post by ycahome »

hello

this error raised by urllib when plugin try to fetch URL.
Some questions:
- where are you testing this url ? through raspberry's browser or another PC on your network?
- many times this can be appear sporadically due to URL timeout. Is your device updated even once?
- did you tried to set Debug mode to True? do you see a more helpful error bellow this one?
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest