Domoticz and OPNsense (or the other way around)
This short article is a follow-up to my previous one on using the OPNsense API.
I'll quickly show how I am using Domoticz to easily change the gateway used by a device (in my case my laptop) from my main ADSL connection to a 4G one, by calling the OPNsense API.
Let's do it "bullet points" mode:
Copy the shell script I showed you in the previous article (in the "Script it, because I'm lazy" section) on your Domoticz server, using
scp.
There's a 'scripts' folder inside the domoticz folder;Make sure the script is executable with a
chmod +x <script.sh>.Optional: create a
shellsubfolder and put the shell script inside, to have a cleaner structure.In Domoticz, go into the
Switchessection, and add a switch by clicking onManual Light/Switch:
Create it with the below configuration:

- Select
Dummy switchas Hardware, - Give it a name,
Switch TypeisOn/OffTypeis not important, as it's a dummy switch,- You may have to change
House CodeandUnit Codeif they're already used by other switches (Domoticz will report a red message "Switch already exists!"). - Once finished, click on
Create Deviceto create it.
- Select
Your newly created switch will appear in the list - click on
Edit:

You can change the icon if you like (I used the one with a laptop, since I'll be using it for that purpose).
Then fill theOn Actionfield with the path to your script, as well as input parameters./!\ you must have
script://in front of the path.Yes, that 3 slashes in total - In my case, that's:
script:///home/pi/domoticz/scripts/shell/opnsense_4g_general.sh add <IP> <alias>and the same for the
Off Action(with adelinstead ofadd):
script:///home/pi/domoticz/scripts/shell/opnsense_4g_general.sh add <IP> <alias>
Of course, you should test using SSH that your script does what it's supposed to do (i.e. either add or remove the specified IP address to/from the OPNsense alias).
Once saved, test your switch by clicking on the laptop icon, which should trigger the script, switch the gateway in OPNsense and also change the laptop icon.
Thanks for reading, and let me know if you found this useful!
Tags: Automation, IT, Opensource