Search | Sailfish OS | Running | PineTime | All Posts

Domoticz and OPNsense (or the other way around)

May 02, 2020 — Nico Cartron

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:

  1. 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 shell subfolder and put the shell script inside, to have a cleaner structure.

  2. In Domoticz, go into the Switches section, and add a switch by clicking on Manual Light/Switch:

  3. Create it with the below configuration:

    • Select Dummy switch as Hardware,
    • Give it a name,
    • Switch Type is On/Off
    • Type is not important, as it's a dummy switch,
    • You may have to change House Code and Unit Code if they're already used by other switches (Domoticz will report a red message "Switch already exists!").
    • Once finished, click on Create Device to create it.
  4. Your newly created switch will appear in the list - click on Edit :

  5. 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 the On Action field 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 a del instead of add):
    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).

  6. 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


I don't have any commenting system, but email me (nicolas at ncartron dot org) your comments!
If you like my work, you can buy me a coffee!