Can I force the OpenVPN client to one specific interface?

I have a setup in my car where a 4G modem takes care of the connection when I'm away from home, while the onboard wifi on the Pi3 that's running OpenWRT connects to my home network when it can and take over. It has priority over the 4G modem to make sure I don't use my cell data when I dont need to.

To have a safe way to access the stuff I have in the car (J.River Media Center, Node-RED, Home Assistant) I have set up an OpenVPN connection to SoftEther, which is running on my home server. This has a static IP set in the tap0 device, so when I connect to it from my home, the IP will still always be 192.168.2.105.

When the car is parked at home, DHCP gives the wifi interface the same address, 192.168.2.105, so I can still connect with the same IP without changing the IP in the apps I use.
Normally this should not be a problem. But it can be, because sometimes when the car comes home and the wifi takes over, and OpenVPN won't quit. I can see in the SoftEther app that the tunnel is disconnected, but I guess OpenVPN is still trying and not releasing, while getting nowhere. I guess you can't really run a tunnel to the WAN of a server when the device that tries to connect is on the LAN of the server.

So is it possible to force the OpenVPN tunnel to disconnect when data is not going through 4G?

Maybe you can make a hotplug script which stops the OpenVPN when the Pi connected to your home via wifi?
See: https://openwrt.org/docs/guide-user/base-system/hotplug

Thanks! That's very possible! And then starts it again when the Pi disconnects from wifi. I need to see if I can even understand that page in the docs, it seemed like quite the handfull. I think, if I understand it correctly, that ifup and ifdown may be what I'm looking for, but I'm not sure.

Possibly, I use hotplug script to do some PBR when a VPN interface comes up, copy the script to /etc/hotplug.d/iface and this script will get executed on interface changes.

The script has something like:

	if [ "$INTERFACE" == "$VPNINTERFACE" ]; then
		if [ "$ACTION" == "ifup" ]; then
			# Actions when the interface is up
        fi
		# remove table and rules
		if [ "$ACTION" == "ifdown" ]; then
			#remove your stuff on ifdown
		fi
	fi

I am not the greatest expert so can not write it for you

I'm sure you know a lot more than me, but thanks again! I'll try to see if I can do something with that.

Edited because of wrong thinking... I can't ping anything when this problem occurs!

Damn, I forgot one thing: I have another network where I connect, at my cabin, and has the same SSID, but another network segment. That would be a WAN for this OpenVPN connetion. So I think I need to check the IP address of the wifi and connect or disconnect OpenVPN based on if the wifi IP address is 192.168.2.105 or any 192.168.0.x address.

I reverted to what I know, Node-RED. I let the Pi that is connected to WAN via the OpenWRT run the SSH command ifconfig phy1-sta0 to the OpenWRT Pi every 15 seconds. i it's connetd to the 192.168.2.x network, the answer will contain 192.168.2.105. In that case it will send the SSH command /etc/init.d/openvpn stop and when it's not connected to that network it will send the SSH command /etc/init.d/openvpn start. I have no idea if it stresses the OpenWRT Pi to do this every 15 seconds and most of the time just send start or stop to something already in that state, so I added a check with /etc/init.d/openvpn statusto see if OpenVPN was active or inactive. So if the state is the same as the one I want it to be, it stops there. If not, it runs the start or stop command.