Make dom0 notifications working in bash script

Hi,

I want this bash script to send notifications when battery reaches 40% and 80%.
#!/bin/bash

# Set the thresholds
LOW_THRESHOLD=40
HIGH_THRESHOLD=80

# set the display and d-bus session address
export DISPLAY=:0
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus

while true; do
	# Get the current battery level
	BATTERY_LEVEL=$(cat /sys/class/power_supply/BAT0/capacity)
	
	# Get the current charging status
	CHARGING_STATUS=$(cat /sys/class/power_supply/BAT0/status)

	# Notify to plug in the charger
	if [[ $BATTERY_LEVEL -le $LOW_THRESHOLD && $CHARGING_STATUS != "Charging" ]]; then
		notify-send "Battery Low" "Battery level is ${BATTERY_LEVEL}%. Plug in AC."
	fi

	# Notify to unplug the charger
	if [[ $BATTERY_LEVEL -ge $HIGH_THRESHOLD && ($CHARGING_STATUS == "Charging" || $CHARGING_STATUS == "Not charging" || $CHARGING_STATUS == "Waiting to discharge") ]]; then
		notify-send "Battery High" "Battery level is ${BATTERY_LEVEL}%. Unplug AC."
	fi

	# Wait 5 minutes before checking again
	sleep 300
done
Also I have a service for it
[Unit]
Description=Battery Notification Service
After=multi-user.target

[Service]
Type=simple
ExecStart=/usr/local/bin/battery_notification.sh
Environment="DISPLAY=:0"
Environment="DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus"
# this is my username example
User=batteryuser

ProtectSystem=full
ProtectHome=true
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target

And it still gives me an error after running

systemctl status battery-notification.service

in dom0’s terminal:

Jul 09 10:15:33 dom0 battery_notification.sh[7320]: Cannot autolaunch D-Bus without X11 $DISPLAY

Do you know how to fix it? :slight_smile:

Copy the systemd unit to ~/.config/systemd/user of your normal user home directory and enable it with systemctl --user enable SERVICENAME

And I believe you will be able to delete the unnecessary Environment variables from systemd unit