Webhook / API / Domoticz

Have you done something on your NEMS server to make it more usable for yourself? Want to share your code? I'd love for you to share and may include your ideas in future releases of NEMS.
Post Reply
mf76130
Junior Member
Posts: 6
Joined: Tue Sep 29, 2020 7:55 pm

Webhook / API / Domoticz

Post by mf76130 »

I wanted some devices in Domoticz to get restarted in case they are recognised as down by Nems.
I ended up with a simple python script with a HTTP Post to my local domoticz.

All is done on raspberry Pi

Prerequisits:
Install "requests" on your nems
connect with SSH to your pi
then logon as the user nagios:

Code: Select all

sudo -u nagios bash
run

Code: Select all

pip3 install requests
to add the py module requests, in case pip3 is missing run

Code: Select all

sudo apt-get install pip3
thats it on the SSH side.

connect with a FTP client to your Nems Pi (example WinSCP) and navigate to the folder "/usr/lib/nagios/plugins"
Here you can find all scripts that you can use in NEMS.
Paste here a new file "Domoticz-restart.py" - whatevername you prefer and then add your script into that, you can also prepare it on your pc and then just move the full file.
Give every user/group permission to run that file!

Here is my code (username and password and IP you need to exchange according to your system)

Code: Select all

import requests
import sys
device=sys.argv[1]
idxl=len(sys.argv)
domoticz_url='http://username:[email protected]:8080/json.htm?type=command&param=switchlight&idx={}&switchcmd=Off'.format(device)
response=requests.post(url=domoticz_url)
domoticz_url='http://username:[email protected]:8080/json.htm?type=command&param=switchlight&idx={}&switchcmd=On'.format(device)
response=requests.post(url=domoticz_url)
On your Nems Server go to Nems Conf and there select the "Misccommands", add a new one, label it as you like and
enter the command line like this:
python3 $USER1$/Domoticz-restart.py "$HOSTALIAS$"

"Domoticz-restart.py" here comes your scriptname and you exchange the argument as well. I have the IDX of the Domoticz device stored as the Alias, so i send that to shut down the device in netim and then turn on again.

You can adapt the scripts and commands as you like and use that for a lot other API calls.

Have fun
Michael
Post Reply