Created
January 26, 2020 15:12
-
-
Save MoLow/c0797ca1926e4dfdd60f5c4d0a7a8aec to your computer and use it in GitHub Desktop.
NUT UPS for Home Assistant
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
sensor: | |
- platform: nut | |
username: upsmon | |
password: secret | |
resources: | |
- ups.load | |
- input.voltage | |
- battery.charge | |
- battery.runtime | |
- platform: template | |
sensors: | |
nut_ups_runtime_friendly: | |
friendly_name: 'Time Remaining' | |
value_template: >- | |
{% set time = (states.sensor.nut_ups_battery_runtime.state | int) | int %} | |
{% set minutes = ((time % 3600) / 60) | int %} | |
{% set hours = ((time % 86400) / 3600) | int %} | |
{% set days = (time / 86400) | int %} | |
{%- if time < 60 -%} | |
Less than a minute | |
{%- else -%} | |
{%- if days > 0 -%} | |
{{ days }}d | |
{%- endif -%} | |
{%- if hours > 0 -%} | |
{%- if days > 0 -%} | |
{{ ' ' }} | |
{%- endif -%} | |
{{ hours }}h | |
{%- endif -%} | |
{%- if minutes > 0 -%} | |
{%- if days > 0 or hours > 0 -%} | |
{{ ' ' }} | |
{%- endif -%} | |
{{ minutes }}m | |
{%- endif -%} | |
{%- endif -%} | |
automation: | |
- id: no_electricity | |
alias: No electricity | |
trigger: | |
- platform: numeric_state | |
entity_id: sensor.nut_ups_input_voltage | |
below: '120' | |
action: | |
service: notify.twilio | |
data_template: | |
message: 'Electricity is down. time remaining until newtworking shutdown: {{ states("sensor.nut_ups_runtime_friendly") }}' | |
target: | |
- '+972050000000000' |
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
version: "3.1" | |
services: | |
nutups: | |
privileged: true | |
restart: always | |
build: nut-upsd | |
ports: | |
- 3493:3493 | |
volumes: | |
- /dev/:/dev/ |
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/sh -ex | |
if [ -z "$API_PASSWORD" ] | |
then | |
API_PASSWORD=$(dd if=/dev/urandom bs=18 count=1 2>/dev/null | base64) | |
fi | |
if [ -z "$ADMIN_PASSWORD" ] | |
then | |
ADMIN_PASSWORD=$(dd if=/dev/urandom bs=18 count=1 2>/dev/null | base64) | |
fi | |
cat >/etc/nut/ups.conf <<EOF | |
[$UPS_NAME] | |
desc = "$UPS_DESC" | |
driver = $UPS_DRIVER | |
port = $UPS_PORT | |
pollinterval = 15 | |
EOF | |
cat >/etc/nut/upsd.conf <<EOF | |
LISTEN 0.0.0.0 3493 | |
EOF | |
cat >/etc/nut/upsd.users <<EOF | |
[admin] | |
password = $ADMIN_PASSWORD | |
actions = set | |
actions = fsd | |
instcmds = all | |
[monitor] | |
password = $API_PASSWORD | |
upsmon master | |
EOF | |
cat >/etc/nut/upsmon.conf <<EOF | |
MONITOR $UPS_NAME@localhost 1 monitor $API_PASSWORD master | |
DEADTIME 25 | |
MAXAGE 25 | |
SHUTDOWNCMD "$SHUTDOWN_CMD" | |
EOF | |
chgrp -R nut /etc/nut /dev/bus/usb | |
chmod -R o-rwx /etc/nut | |
/usr/sbin/upsdrvctl start | |
/usr/sbin/upsd | |
exec /usr/sbin/upsmon -D |
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
FROM alpine:3.10 | |
LABEL maintainer="[email protected]" | |
ENV NUT_VERSION 2.7.4 | |
ENV UPS_NAME="ups" | |
ENV UPS_DESC="UPS" | |
ENV UPS_DRIVER="usbhid-ups" | |
ENV UPS_PORT="auto" | |
ENV API_PASSWORD="" | |
ENV ADMIN_PASSWORD="" | |
ENV SHUTDOWN_CMD="echo 'System shutdown not configured!'" | |
RUN set -ex; \ | |
# run dependencies | |
apk add --no-cache \ | |
openssh-client \ | |
libusb-compat \ | |
; \ | |
# build dependencies | |
apk add --no-cache --virtual .build-deps \ | |
libusb-compat-dev \ | |
build-base \ | |
; \ | |
# download and extract | |
cd /tmp; \ | |
wget http://www.networkupstools.org/source/2.7/nut-$NUT_VERSION.tar.gz; \ | |
tar xfz nut-$NUT_VERSION.tar.gz; \ | |
cd nut-$NUT_VERSION \ | |
; \ | |
# build | |
./configure \ | |
--prefix=/usr \ | |
--sysconfdir=/etc/nut \ | |
--disable-dependency-tracking \ | |
--enable-strip \ | |
--disable-static \ | |
--with-all=no \ | |
--with-usb=yes \ | |
--datadir=/usr/share/nut \ | |
--with-drvpath=/usr/share/nut \ | |
--with-statepath=/var/run/nut \ | |
--with-user=nut \ | |
--with-group=nut \ | |
; \ | |
# install | |
make install \ | |
; \ | |
# create nut user | |
adduser -D -h /var/run/nut nut; \ | |
chgrp -R nut /etc/nut; \ | |
chmod -R o-rwx /etc/nut; \ | |
install -d -m 750 -o nut -g nut /var/run/nut \ | |
; \ | |
# cleanup | |
rm -rf /tmp/nut-$NUT_VERSION.tar.gz /tmp/nut-$NUT_VERSION; \ | |
apk del .build-deps | |
COPY src/docker-entrypoint /usr/local/bin/ | |
ENTRYPOINT ["docker-entrypoint"] | |
WORKDIR /var/run/nut | |
EXPOSE 3493 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment