As I am using a notebook as my main device now, I needed to automate some stuff when changing workplaces.
I wanted two things to happen:

  1. Turn off the notebooks internal screen while it is docked to it’s docking station
  2. Turn on the external keyboards backlight when connected

To achieve the first goal one needs two things:

  1. a script that manages the monitor settings
  2. a udev-rule that executes that script

The script has to be called with a command line argument. If this is docked the notebook’s internal monitor becomes disabled and two other monitors enabled. If it is undocked the notebook’s internal monitors is enabled.
I am using swaywm as my window manager and scripts called by udev are not executed as guid 1000, or in other words as your user, the SWAYSOCK variable has to be exported first.
For some reason there where two sway-sockets listed under /run/user/1000/ on my system. In my case the one to be used was the second one (… | sed -n 2p).

# !/bin/bash

# The sway-socket needs to be exported here because the script is not run as uid 1000 
export SWAYSOCK=$(ls /run/user/1000/sway-ipc*.sock | sed -n 2p)

# Arange monitors
if [ $1 == "docked" ]; then
   swaymsg output DP-1 pos 0 0
   swaymsg output HDMI-A-1 pos 1920 0
   swaymsg output eDP-1 disable
elif [ $1 == "undocked" ]; then
   swaymsg output eDP-1 enable
else
   echo "Please provide an argument."
fi

The udev rule simply calls the script either the argument docked, if the docking station is attached, or with undeocked, if the docking station is detached.

ACTION=="add", SUBSYSTEMS=="usb", ATTR{idVendor}=="<vendor id>", ATTR{idProduct}=="<product id>", MODE="0660", RUN+="/bin/bash <path to script> docked"
ACTION=="remove", SUBSYSTEMS=="usb", ATTR{idVendor}=="<vendor id>", ATTR{idProduct}=="<product id>", MODE="0660", RUN+="/bin/bash <path to script> undocked"

The remove-action does not always work with all attributes as these might not be accasseble by the time of removal any more. One can run the following line of code to find out which attributes will work with a remove-action.
udevadm monitor --environment --udev > udev_monitor.log

The udev rule has to be placed at /etc/udev/rules.d/. Make shure to give the proper suffix to it (e.g. docking.rules).
To enable it one has to execute the following line:

sudo udevadm control --reload && sudo udevadm trigger