Webhook / API / Domoticz
Posted: Mon Apr 04, 2022 2:00 pm
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:
run to add the py module requests, in case pip3 is missing run
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)
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
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
Code: Select all
pip3 install requests
Code: Select all
sudo apt-get install pip3
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¶m=switchlight&idx={}&switchcmd=Off'.format(device)
response=requests.post(url=domoticz_url)
domoticz_url='http://username:[email protected]:8080/json.htm?type=command¶m=switchlight&idx={}&switchcmd=On'.format(device)
response=requests.post(url=domoticz_url)
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