Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional sensor prefix for home assistant entities #5

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sysmon.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ HASS_MQTT_PREFIX="homeassistant"
MQTT_PUB_PATH="/home/servers"
MQTT_PUBLISH_PERIOD=15

# Define sensor prefix. Options: 0 = none (default and if not 1 or string), 1 = hostname, custom if string
SENSOR_PREFIX_OPTION=0

# name of network interface (e.g. eth0) otherwise first active one will be used
NETWORK_IFACE=""

Expand Down
20 changes: 19 additions & 1 deletion sysmon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ HASS_MQTT_PREFIX="homeassistant"
MQTT_PUB_PATH="/home/servers"
MQTT_PUBLISH_PERIOD=20

# Define sensor prefix. Options: 0 = none (default and if not 1 or string), 1 = hostname, custom if string
SENSOR_PREFIX_OPTION=0

# name of network interface (e.g. eth0) otherwise first active one will be used
NETWORK_IFACE=""

Expand Down Expand Up @@ -128,6 +131,17 @@ temperature_sensor_names() {
done
}

sensor_prefix_generator() {
if [ "$SENSOR_PREFIX_OPTION" = "0" ]; then
SENSOR_PREFIX=""
elif [ "$SENSOR_PREFIX_OPTION" = "1" ]; then
SENSOR_PREFIX=$(cat /proc/sys/kernel/hostname)
else
SENSOR_PREFIX=$SENSOR_PREFIX_OPTION
fi
SENSOR_PREFIX=$(echo $SENSOR_PREFIX | sed "s/[^a-zA-Z0-9']/_/g" | sed "s/\_\{1,\}/_/g" | sed "s/[A-Z]/\L&/g")
}

##### Sensor functions #####

CPU_COUNT=$(grep -c ^processor /proc/cpuinfo)
Expand Down Expand Up @@ -522,6 +536,11 @@ start() {

info "using device name: $DEVICE_NAME"

sensor_prefix_generator
if ! [ -z "$SENSOR_PREFIX" ]; then
info "using sensor prefix: $SENSOR_PREFIX"
fi

setup
publish_discovery_all

Expand All @@ -542,4 +561,3 @@ else
fi

start