Last active
October 31, 2021 05:24
-
-
Save hurricup/16c056d4081dc15bae8d077e14b1029c to your computer and use it in GitHub Desktop.
MQTT-based voltage daemon built on Network UPS Tools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#set -eu | |
ha_prefix="homeassistant" | |
ha_config_suffix="config" | |
ha_state_suffix="state" | |
ha_component="sensor" | |
ha_object_id="ac_voltage_meter_powercom" | |
ha_common_prefix=$ha_prefix/$ha_component/$ha_object_id | |
ha_config_topic=$ha_common_prefix/$ha_config_suffix | |
ha_state_topic=$ha_common_prefix/$ha_state_suffix | |
ha_config=$(cat <<JSON | |
{ | |
"name": "AC Input Voltage", | |
"unique_id": "$ha_object_id", | |
"state_topic": "$ha_state_topic", | |
"device_class": "voltage", | |
"state_class": "measurement", | |
"unit_of_measurement": "V" | |
} | |
JSON | |
) | |
echo Using config topic: $ha_config_topic | |
echo "Using config: $ha_config" | |
mosquitto_pub -r -t "$ha_config_topic" -m "$ha_config" | |
previous_voltage=-1 | |
while true | |
do | |
upsinfo=$(upsc powercom 2>/dev/null |grep 'input.voltage:') | |
voltage=${upsinfo#input.voltage: } | |
voltage=${voltage%.0} | |
if [ $voltage != $previous_voltage ] | |
then | |
echo "Publishing $voltage" | |
mosquitto_pub -t $ha_state_topic -m $voltage | |
previous_voltage=$voltage | |
fi | |
sleep 1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Logger for Home Assistant:
Requires: