Domoticz Pings any IP in LAN, and switching a device, using Perl

From Domoticz
Jump to: navigation, search

Switching a device by any local IP

Under constrution'
Domoticz can ping your IP devices using a perl script.
By creating a dummy switch, Domoticz monitor a lot of devices.
Domoticz can automatically switch (via this script) your IP devices on or off.

Telephones, ipcameras, sip, blueray, satbox, etc - if it has an ip address it is possible to ping it.
When such devices go off or on, you can e.g. send a message or perform any other Domoticz task.
Or, if your kids are sitting too long at the computer, you can transmit (through events) a message. Also my solar meters can now be quite simply monitored.

How To

Make sure that the IP addresses of your IP devices have been fixed.
Otherwise, make sure a static IP is always assigned to the same mac address(es)

Create a dummy hardware device, for example ip_ping.
Domoticz doesn't know that the hardware or its devices exists, so we need to add virtual devices in Domoticz that act as our ip_ping devices.

  • Browse to your Domoticz installation and select Setup --> Hardware.
  • Select Type: Dummy, enter a name for the dummy hardware and press Add.
  • To add a new switch navigate to the Switches page and press Manual Light/Switch.
  • Select the dummy hardware you created earlier and give your switch a name. Select Switch Type: On/Off. The Type, House Code and Unit Code shouldn't be of any importance. Finally press Add Device
  • Go to Setup --> Devices and note down the Idx of your newly-created switch. You will need this later.

Create a dummy switch as mentioned above, and remember the IDX, e.g. 41
Or if you want multiple ip's pinged then create some more dummy switches, for example 44 and 681

Install as pi user:
sudo apt-get install libjson-perl libdatetime-perl libwww-perl

Place a crontab, as pi user on your RPi:
crontab -e
*/1 * * * * /home/pi/domoticz/scripts/ 2>&1 >> /dev/null
Edit the 1 to the time [ minutes ] you like that Domoticz pings your Ip
Ctrl O and enter to save
Ctrl X to exit

Create a document in: /home/pi/domoticz/scripts/
And call it:
Or in one time:
sudo nano /home/pi/domoticz/scripts/
Place the code below in the document

    use v5.14;
    use LWP::Simple;                # From CPAN
    use JSON qw( decode_json );     # From CPAN
    use Data::Dumper;               # Perl core module
    use strict;                     # Good practice
    use warnings;                   # Good practice
    use utf8;
    use feature     qw< unicode_strings >;
    # Configuration section, please update to your values
    my $domoticz = "";  # ip and port of your Domoticz server 
    my $domo_cmd = "http://$domoticz/json.htm?type=devices&filter=all&used=true&order=Name";
            # Array of (device idx, IP)
                      my %IP=(41=>'',	# Ip 1
                              44=>'',	# Ip 2
                             681=>'');      # Ip 3
    my $debug=0;
    # Get the JSON url
    my $json = get( $domo_cmd );
    die "Could not get $domo_cmd!" unless defined $json;
    # Decode the entire JSON
    my $decoded = JSON->new->utf8(0)->decode( $json );
    my @results = @{ $decoded->{'result'} };
    #Put JSON switch and status in a Table
    my @tab;
    foreach my $f ( @results ) {
      if ($f->{"SwitchType"}) {
    # Now we go all over the IP to check if they are alive
    foreach my $k (keys %IP) {
            my $ip=$IP{$k};
            my $res=system("sudo ping $ip -w 3 2>&1 > /dev/null");
    #print $k." ".$res."\n";
            if (($res==0)&&($tab[$k] eq 'Off')) {
                    #If device answered to ping and device status is Off, turn it On in Domoticz
                    if ($debug) {print "$k is On\n"};
                   `curl -s "http://$domoticz/json.htm?type=command&param=switchlight&idx=$k&switchcmd=On"`;
            } elsif (($res!=0)&&($tab[$k] eq 'On')) {
                    #If device did NOT answer to ping and device status is On, turn it Off in Domoticz
                    if ($debug) {print "$k is Off\n"};
                   `curl -s "http://$domoticz/json.htm?type=command&param=switchlight&idx=$k&switchcmd=Off"`;
            } else {
                    if ($debug) {print "do nothing: $k is ".$tab[$k]."\n";}

Check the document in your RPi /home/pi/domoticz/scripts, with a program like Winscp
Make sure that the code #!appears in the upper left corner of the document!
Customize this document so that all your ip's and your idx are correct.
Also check if your ip and port of your Domoticz RPi are correct.
Make sure you save everything!!

Make this script executable:
cd /home/pi/domoticz/scripts
sudo chmod +x

cd /home/pi/domoticz/scripts

If you made no errors, it should all work well.
If there are any errors, check all your settings.
YOU did something WRONG, not ME.... :-)

Link to topic


BIG Thanks to:

epierre and nickasimir

Also check out

System Alive Checker (Ping)