Python - Monitor Domoticz IP address

From Domoticz
Revision as of 23:07, 22 December 2020 by Rwaaren (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Purpose

This script queries the current device for its IP address and records it to a Virtual Text Sensor within Domoticz. It has been useful in environments with headless devices (e.g. Raspberry Pi Zero) where fixed IP addresses are unavailable and the device's IP address is subject to change. The IP address record will only update within Domoticz if it has changed from the stored value.

This script will update a virtual text sensor to monitor the IP address of this device:

  • IP Address

Dependencies - hardware / software / operating system

  • Linux
  • Python

Steps

  • Within Domoticz open the Setup > Hardware screen
  • Create 1 Virtual Sensor with the "Text" Type named:
    • IP Address
  • Within Domoticz open the Setup > Devices screen and note the IDX of the new Virtual Sensor added, you'll need this in the Python script
  • Make sure that in the Security section in the settings (Setup > Settings > System > Local Networks (no username/password) you allow 127.0.0.1 (and / or ::1 when using IPv6 ) to not need a password.
  • Create a new file within /domoticz/scripts/python/ called ip_address.py
  • Change the permissions on ip_address.py to allow it to run from crontab
  • Edit the ip_address.py file
  • Copy and paste the contents of the Python script section below
  • Change the following fields:
    • switchID
    • domoticzPort
    • domoticzIP
    • linkName
  • Save changes and run the Python script, if successful after a moment it will display your IP address
  • Within Domoticz open the Utility tab and check that data is being recorded
  • If all OK include it in crontab

Python script (python2)

#!/usr/bin/python  

#
# Script to update domoticz text sensor with IP address (when changed) 
# requires python2
#

import datetime
import os
import urllib2
import json

switchID = '1192'           # text sensor idx
domoticzPort = "8084"       # your port 
domoticzIP = "localhost"    # or 127.0.0.1 or real IP
linkName = 'eth0'           # or wlan0 or eno1 or ..

domoticz= domoticzIP + ":" + domoticzPort
ipCommand = 'ip addr show ' + linkName

def log(message):
  print (message)

def domoticzrequest (url):
  request = urllib2.Request(url)
  response = urllib2.urlopen(request)
  return response.read()


domoticzurl = "http://" + domoticz + "/json.htm?type=devices&rid=" + switchID

json_object = json.loads(domoticzrequest(domoticzurl))                             # retrieve sensor data
if json_object["status"] == "OK":
  if json_object["result"][0]["idx"] == switchID:
    existing_ipv4 = json_object["result"][0]["Data"]
    lastUpdate = json_object["result"][0]["LastUpdate"]
    device = json_object["result"][0]["Name"]

ipv4 = os.popen(ipCommand).read().split("inet ")[1].split("/")[0]  # get IP number (IPv4)
if ipv4 == existing_ipv4:
  log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + ": " + ipv4 + ", status unchanged since " + lastUpdate)
else:
  domoticzrequest("http://" + domoticz + "/json.htm?type=command&param=udevice&idx=" + switchID + "&nvalue=0&svalue=" + ipv4) # update device
  log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + ": " + ipv4 + ", status updated")

Python script (python3)

#!/usr/bin/python  

#
# Script to update domoticz text sensor with IP address (when changed)
#
import datetime
import os
import urllib.request, urllib.error, urllib.parse
import json

switchID = '1192'           # text sensor idx
domoticzPort = "8080"       # your port 
domoticzIP = "localhost"    # or 127.0.0.1 or real IP
linkName = 'eth0'           # or wlan0 or eno1 or ..

domoticz= domoticzIP + ":" + domoticzPort
ipCommand = 'ip addr show ' + linkName

def log(message):
  print (message)

def domoticzrequest (url):
  request = urllib.request.Request(url)
  response = urllib.request.urlopen(request)
  return response.read()


domoticzurl = "http://" + domoticz + "/json.htm?type=devices&rid=" + switchID
json_object = json.loads(domoticzrequest(domoticzurl))  # retrieve sensor data
if json_object["status"] == "OK":
  if json_object["result"][0]["idx"] == switchID:
    existing_ipv4 = json_object["result"][0]["Data"]
    lastUpdate = json_object["result"][0]["LastUpdate"]
    device = json_object["result"][0]["Name"]

ipv4 = os.popen(ipCommand).read().split("inet ")[1].split("/")[0]  # get IP number (IPv4)
if ipv4 == existing_ipv4:
  log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + ": " + ipv4 + ", status unchanged since " + lastUpdate)
else:
  domoticzrequest("http://" + domoticz + "/json.htm?type=command&param=udevice&idx=" + switchID + "&nvalue=0&svalue=" + ipv4) # update device
  log (datetime.datetime.now().strftime("%H:%M:%S") + "- " + device + ": " + ipv4 + ", status updated")