arnbme / apcupsd

When mains power is lost, followed by an APC UPS battery depletion, gracefully shutdown a Hubitat hub connected to the UPS, using Windows, VBS and apcupsd.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SmartUPS VBS Version

Table of Contents

 1. Purpose
 2. Requirements
 3. Features
 4. Donate
 5. Installation Overview
 6. SmartUPS driver and VBS modules
 7. Create Virtual Device
 8. Testing
 9. Create a Windows Scheduler Task
10. Install EventGhost
11. Adjust Windows Power Settings
12. Create RM Power Control Rule(s)
13. Restarting the Hub after a graceful shutdown
14. Restarting the Windows system after a shutdown
15. Should I plug my computer into the UPS' battery backup?
16. Uninstalling
17. Get Help, report an issue, or contact information
18. Known Issues

1. Purpose

Perform a graceful Hub shutdown when power is lost.

Developed for, and tested on a Windows 10 system functioning as the interface between the APC UPS and the HE Hub, but may work on any system supporting apcupsd and Visual Basic Script.

This package was derived from Steve Wright's APC UPS Monitor Driver release, but does not use or require a PHP server.

🔼 Back to top

2. Requirements

  • Hubitat Hub plugged into an APC UPS battery backup plug
  • The APC communication cable plugged into a Windows machine's USB port
  • apcupsd.org's package installed on same Windows machine

Should your APC UPS support WiFi consider LG Kahn's release
For Non-windows systems consider Steve Wright's APC UPS Monitor Driver

🔼 Back to top

3. Features

  • Reports UPS Device Events in Hubitat virtual device attribute, lastEvent
    • onbattery - mains power failed
    • offbattery - mains power restored
    • doshutdown - UPS about to shutdown
    • COMMLOST - apcupsd lost UPS communication
    • commok - apcupsd UPS communication restored
  • Sends UPS Device Statistics: every "user defined" minutes, using a repeating Windows Scheduled Task or optional EventGhost webserver plugin.
  • Support modules are Visual Basic Script
  • Executes without being logged in to Windows
  • Remotely reboot Windows machine from HE (Optional requires EventGhost)

🔼 Back to top

4. Support this project

This app is free. However, if you like it, derived benefit from it, and want to express your support, donations are appreciated.

🔼 Back to top

5. Installation Overview

Take a deep breath, hold it, exhale. This is a substantial process.

  1. Uninstall APC PowerChute, if installed

  2. Connect APC UPS supplied cable to a USB port

  3. Connect Hub power connector to a UPS Battery Backup plug

    • Place a Wifi plug between the UPS and the Hub power connector, insuring ability to remotely restart the hub when it's shutdown, but never loses power. I use a TP-Link Kasa plug with the Kasa app.
  4. Install apcupds app, then setup apcupsd

  5. Install module SmartUPS.groovy from Github repository into Hub's Drivers or use the Hubitat Package Manager

  6. Copy the six VBS modules from Github repository to Windows directory C:/apcupsd/etc/apcupsd
    Edit your hub's IP address in module smartUPS.VBS

  7. Create a virtual device using SmartUps driver, then set IP address to your Windows machine IP address. This IP address should be permanently reserved in your router.

  8. Create the RM for Battery and Shutdown

  9. Test the VBS scripts and Hubitat SmartUPS device

  10. Create a Windows Scheduled Task
    OR
    Install EventGhost

  11. Adjust Windows Power setings

  12. Reboot Windows system, then verify smartUPS.vbs is running on your scheduled task timing.

  13. Run a live power shutdown test

  14. Set windows to reboot when power is restored

  15. Review: Should I plug my computer into the UPS' battery backup

🔼 Back to top

6. SmartUPS Driver and VBS modules

There are four VBS scripts and a one Groovy Device Handler (DH) associated with this app stored in a Github respitory. You may also install he Groovy module using the Hubitat Package Manager. The VBS scripts must be copied to C:/apcupsd/etc/apcupsd from Github

  • After or prior to installing smartUPS.vbs
  • After or prior to installing doshutdown.vbs
    • Determine the time in seconds it takes to complete a manual hub shutdown. Set this (shutdown_seconds + 10) * 1000 into the WScript.Sleep statement. The supplied value is 30000, 30 seconds.
Module Name Function Install Location
SmartUPS.groovy UPS device handler. Controls communication from UPS to Hub *Requires Modification Hubitat Drivers
smartUPS.vbs Gets UPS devices statistics and sends information to the Hub, also sends all apcupsd event handler information to the Hub Windows C:apcupsd/etc/apsupsd
onbattery.vbs apcupsd onbattery event handler Windows C:apcupsd/etc/apsupsd
offbattery.vbs apcupsd offbattery event handler Windows C:apcupsd/etc/apsupsd
doshutdown.vbs apcupsd UPS is shutting down handler *Requires Modification Windows C:apcupsd/etc/apsupsd
commfailure.vbs apcupsd communication lost event handler Windows C:apcupsd/etc/apsupsd
commok.vbs apcupsd communication restored event handler Windows C:apcupsd/etc/apsupsd

🔼 Back to top

7. Create and setup a Virtual Device

  • On Hubitat main menu, click Devices
  • Click: Add Virtual Device
  • Set Device Name and Device Label to: APC UPS (or whatever you want)
  • Type: Select User driver - SmartUPS
  • Click button: Save Device

  • Set IP Address of system running apcupsd
  • Click button: Save Preferences

  • Set Event History to 5 (optional)
  • Click button: Save Device

🔼 Back to top

8. Testing

  1. Open a command window

    • Right click on Windows "Start" icon, menu opens
    • Click on Run
    • Enter cmd, click OK, the command window opens
  2. Test smartUPS.vbs as follows

    • in command window enter: cscript C:\apcupsd\etc\apcupsd\smartUPS.vbs then click Enter key
    • verify hub's APC UPS device statistics were updated
  3. Test the three VBS event scripts

    • on command line enter: cscript C:\apcupsd\etc\apcupsd\onbattery.vbs then click Enter key
      Hub device attribute results: PowerSouce: battery; lastEvent: onbattery

    • on command line enter: cscript C:\apcupsd\etc\apcupsd\offbattery.vbs then click Enter key
      Hub device attribute results: PowerSouce: mains; lastEvent: offbattery

    • on command line enter: cscript C:\apcupsd\etc\apcupsd\powerout.vbs then click Enter key
      Hub device attribute results: PowerSouce: battery; lastEvent: onbattery

    • on command line enter: cscript C:\apcupsd\etc\apcupsd\doshutdown.vbs then click Enter key
      Hub device attribute results: PowerSouce: battery; lastEvent: failing
      When the RM example rule is installed the hub gracefully shuts down. Restart the Hub with a power cycle: power off, power on.

  4. Live testing a power outage This should gracefully shutdown your windows machine and Hubitat Hub

    • The RM power rules in section 11 must be coded and active before doing this test
    • For safety make backups of your hub and windows machine. I've tested many times without doing this, but you never know.
    • Make a backup copy of file C:/apcupsd/etc/apcupsd/apcupsd.conf
    • Edit file C:/apcupsd/etc/apcupsd/apcupsd.conf
    • Change setting: BATTERYLEVEL to 95
    • Change setting: MINUTES to your current UPS remaining time - 5 minutes
    • Save the file
    • You will either have to reboot, or stop and restart apcupsd to apply the settings. I've only been successful with rebooting.
    • Remove power from the UPS, WAIT.
    • After a succesful test, repower the UPS, power cycle the HUB, restart windows.
    • Edit file C:/apcupsd/etc/apcupsd/apcupsd.conf
    • Change BATTERYLEVEL to 5, MINUTES to 3, Save
    • Reboot or cycle apcupsd to apply settings

🔼 Back to top

9. Create a Windows Scheduled Task

Note: Event based Hub shutdown works without the scheduled task. However, the Windows Scheduled Task or EventGhost is required should you want to shutdown the HUB based upon battery percentage or other UPS status fields, or want UPS statistics displayed in the driver.

  1. Open the Windows Task Scheduler
    • Click on Windows task bar "search" icon
    • Enter: Task Scheduler
    • Click on Run

  2. Task Scheduler opens
    • On right side of screen, Click on Create Task
    • Set name: SmartUps
    • Select "run whether user is logged in or not"
    • On top of window click Triggers, then click New

  3. Set Triggers
    • Set Begin the task selector to: At Startup
    • Check Delay task for, key in 3 minutes
    • Check Repeat task for, 10 minutes, for a duration of: Indefinitely
    • Enabled should be set, Set enabled if not
    • Click OK
    • On top of screen click Actions, then click New

  4. Set Actions
    • Set Program/script to: cscript
    • Set Arguments to C:\apcupsd\etc\apcupsd\smartUPS.VBS
    • click OK
    • On top of screen click Conditions

  5. Set Conditions
    • Uncheck Start the task only if computer is on AC power

  6. Set Settings
    • Set "If task fails. restart every" to 5 Minutes
    • Set "Repeats" to 999
    • Uncheck "Stop task if runs longer than"

  7. Save then test the Task
    • Click OK on bottom of window
    • Enter your windows password (not the pin)
    • The task is created
    • Test it by clicking Run on right side of window

  8. Activate task by Rebooting system

🔼 Back to top

10. Install EventGhost

Note: Event based Hub shutdown works without EventGhost. However, EventGhost or the Windows Scheduled Task is required should you want to shutdown the HUB based upon battery percentage or other UPS status fields, or want UPS statistics displayed in the driver.

  1. Install EventGhost April 30, 2023 The EventGost website is gone. Install by downloading then executing the setup.exe file from the Github repository link above

  2. Activate the Webserver plugin

    • Highlight Autostart, right click Add Plugin, select webserver
    • Unless you know what you are doing leave it at port 80
    • In the HTML document root: field enter C:/www
    • Do not change: Event Prefix: HTML Realm: EventGhost
    • Click OK
    • The www directory name is inconsequential as long as it matches what is entered into the webserver's document root field
  3. Create directory C:/www

    • Create file index.html with the following
    • <HTML><BODY></BODY></HTML>
  4. Create Device Status Macro

  5. Highlight the computer name at the top of Configuration column

  6. create a folder: name is smarttUPS (or your choice)

  7. highlight the folder then Add a Macro in the newly created folder

    • Expand System, select Run Command
    • Enter the windows command cscript C:/apcupsd/etc/apcupsd/smartUPS.vbs
    • Click Test
    • When it works, click OK
    • This is now a Macro, with an Action
  8. From a web browser enter 127.0.0.1?HE.smartUPS

      • If you get a 404, not found, see steps 2 and 3 above
  9. In the EventGhost event log you should see an event: HTTP.smartUPS[]

  10. Drag the HTTP event to the newly created Macro. It may look like it's not going, but once you stop dragging it will show up in the macro. That is the trigger

  11. Reenter the URL 127.0.0.1?HE.smartUPS from the browser, enter

  12. Script smartUPS.vbs should run

  13. Click the refresh button in the SmartUPS device page

  14. The script should run

  15. Setup EventGhost to run without a login

    • Use directions in Section 9 above to create a new Windows Scheduled Task
    • 2 Name Start EventGhost
    • 3 Triggers: At startup plus 2 minutes, no repeats
    • 4 Action: "C:\Program Files\EventGhost\EventGhost.exe"
    • 5, 6, 7 Same.
    • Save task. Reboot, wait 2 minutes, test by using another device to issue the Refresh command
    • Note adjust 2 minute timer value based upon elapsed time for windows startup
  16. Setup Windows Reboot Macro

  17. Highlight the SmartUPS folder, then Add a Macro in the newly created folder

    • Expand System, select Run Command
    • Enter the windows command: shutdown.exe /f /r /t 0
    • Save it, Click test, windows should reboot
    • return to EventGhost after reboot
    • This is now a Macro, with an Action
  18. From a web browser enter 127.0.0.1?HE.rebootWindows

      • If you get a 404, not found, see steps 2 and 3 above
  19. In the EventGhost event log you should see an event: HTTP.rebootWindows[]

  20. Drag the HTTP event to the newly created Macro. It may look like it's not going, but once you stop dragging it will show up in the macro. That is the trigger

  21. Save or lose it on reboot

  22. Reenter URL 127.0.0.1?HE.rebootWindows from the browser, enter

  23. Window should restart, return to EventGhost

  24. Click the rebootWindows button in the SmartUPS device page to test

🔼 Back to top

11. Adjust Windows Power Settings

  • On Power and Batteries change "Put Computer to Sleep" to Never Unless you can figure out a way to wake the machine as needed

🔼 Back to top

12. Prepare RM Power Control rules

image RM Power image RM Power

image RM Power

🔼 Back to top

13. Restarting the Hub after a graceful shutdown

A Wifi plug between the UPS plug and the HE Hub power connector allows for a remote hub restart in case the Hub must be power cycled: power off, power on; to restart after a graceful shutdown when power is restored. This occurs when the Hub is gracefully shutdown, but never loses power.

When the Hub loses power, it will automatically restart when power is restored.

14. Restarting the Windows system after a shutdown

This requires changing the computer's BIOS settings

Some links

🔼 Back to top

15. Should I plug my computer into the UPS' battery backup?

Computer has BatteriesComputer Plugged into UPSResults
1.YesYesIdeal, computer shuts down at event 'failing" or when computer batteries reach low level after "failing" event
2.YesNoMay work, may not. Computer's batteries must last longer than UPS power
3.NoNoIt will never work, computer dies at power failure
4.NoYesWorks, computer shuts down at event 'failing"

🔼 Back to top

16. Uninstalling

  1. Delete Windows scheduled task
  2. Uninstall apcupsd from Windows
  3. Uninstall EventGhost from Windows
  4. Remove SmartUPS virtual device
  5. Remove SmartUPS from Devices code

🔼 Back to top

17. Get Help, report an issue, and contact information

🔼 Back to top

18. Known Issues

  • The SmartUPS device commands Refresh and rebootWindows do nothing when EventGhost is not installed and setup.
  • After a graceful shutdown, followed by Windows and Hub reboot, Fully dashboards do not update. Solution: Click the checkmark on the dashboard screen or use the following image RM refresh
  • Hub does not complete shutdown prior to power shutoff
    • Adjust file doshutdown.vbs: change Wscript.sleep value to a a higher number. It's milliseconds.
    • Try setting apcupsd.conf ANNOY to a higher number
    • Try setting apcupsd.conf KILLDELAY to something

🔼 Back to top

About

When mains power is lost, followed by an APC UPS battery depletion, gracefully shutdown a Hubitat hub connected to the UPS, using Windows, VBS and apcupsd.


Languages

Language:Groovy 72.5%Language:VBScript 27.5%