Skip to content

Instantly share code, notes, and snippets.

@hurricup
Last active October 31, 2021 05:24
Show Gist options
  • Save hurricup/16c056d4081dc15bae8d077e14b1029c to your computer and use it in GitHub Desktop.
Save hurricup/16c056d4081dc15bae8d077e14b1029c to your computer and use it in GitHub Desktop.
MQTT-based voltage daemon built on Network UPS Tools
#!/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
@hurricup
Copy link
Author

Logger for Home Assistant:

Requires:

  1. UPS with data channel
  2. Network UPS Tools (NUT)
  3. Eclipse Mosquitto MQTT Brocker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment