Monitoring an APC UPS with node-red and MQTT

apc cs500 upsIntroduction

APC sell a decent range of USB connected Uninterruptable Power Supplies (UPS) such as the CS500 and BE700G.  These are supported by the linux utility apcupsd for control and monitoring.

This post explains how to monitor these UPS devices using Node-Red to obtain and publish data using MQTT.  It assumes you already have a working Node-Red install.

Installing APCUPSD

  1. Install apcupsd for monitoring USB connected UPS:
    apt-get install apcupsd
  2. Edit /etc/default/apcupsd:
    ISCONFIGURED=yes
  3. Edit /etc/apcupsd/apcupsd.conf:
    UPSCABLE usb
    UPSTYPE usb
    DEVICE
    STATTIME 0

Node-Red Flow

Node-Red apcaccess to MQTT

Node-Red Flow for extracting data from apcaccess and publishing to MQTT

This flow runs the command line utility apcaccess every 10 seconds.  It then parses the output into a number of MQTT messages and publishes them to the configured MQTT server on the topic specified in the “Parse apcaccess data” function.

Copy and paste the following into node-red and configure an mqtt broker and topic:


[{"id":"d0b0bd19.2f4f4","type":"mqtt-broker","broker":"mqttbroker","port":"1883","clientid":""},{"id":"eed09608.112f68","type":"exec","command":"/sbin/apcaccess","append":"","useSpawn":"","name":"apcaccess","x":284,"y":183,"z":"7238f002.8dc71","wires":[["e63edac5.19c128"],[],[]]},{"id":"4ca890ab.b3577","type":"inject","name":"every 10 seconds","topic":"","payload":"","payloadType":"none","repeat":"10","crontab":"","once":true,"x":111,"y":182,"z":"7238f002.8dc71","wires":[["eed09608.112f68"]]},{"id":"e63edac5.19c128","type":"function","name":"Parse apcaccess data","func":"var wanted = ['upsname', 'serialno', 'status', 'linev', 'linefreq', 'loadpct', 'battv', 'bcharge', 'timeleft', 'itemp', 'xonbatt', 'xoffbatt'];\nvar lines = msg.payload.trim().split(\"\\n\");\nvar outputMsgs = [];\nvar mqtttopic = 'home/common/ups/CS350';\n\n// loop over every line\nlines.forEach(function (line) {\n    // assign values\n    var stats = line.split(' : ');\n    var label = stats[0].toLowerCase();\n    var value = stats[1];\n\n    // remove surrounding spaces\n    label = label.replace(/(^\\s+|\\s+$)/g, '');\n    context.curValues = context.curValues || {};\n    context.curValues[label] = context.curValues[label] || 0;\n\n    // if found as wanted value, store it\n    if (wanted.indexOf(label) > -1) {\n        value = value.replace(/(^\\s+|\\s+$)/g, '');\n        if (context.curValues[label] != value) {\n            outputMsgs.push({topic:mqtttopic + '/' + label, payload:value});\n            context.curValues[label] = value;\n        }\n    }\n});\n\nreturn [ outputMsgs ];","outputs":1,"x":474,"y":170,"z":"7238f002.8dc71","wires":[["e2ef6210.1d10a"]]},{"id":"e2ef6210.1d10a","type":"mqtt out","name":"","topic":"","qos":"","retain":"","broker":"d0b0bd19.2f4f4","x":677,"y":168,"z":"7238f002.8dc71","wires":[]},{"id":"7359c079.8ca64","type":"comment","name":"Extract data from a connected UPS via apcupsd and post to mqtt topic home/common/ups/<device-name>","info":"","x":392.5,"y":107,"z":"7238f002.8dc71","wires":[]}]

2 thoughts on “Monitoring an APC UPS with node-red and MQTT”

Leave a Reply

Your email address will not be published. Required fields are marked *