Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,


as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.


As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!



Smart Home Forum by FIBARO Team

[VD + SCENE + PHP] APC UPS Monitor and auto shutdown of HC2 JUN-2018

   (1 review)

About This File


I noticed in the Forum that there is some interest for link APC UPS with Fibaro HC2 and have auto shutdown of HC2 if there is no electricity.


I have spent few evenings and I am happy to share what I did.


I want to thank @Sankotronic  for the permission to use part of his code and @Lambik whose icons of UPS I used as a base to create icons representing various status of the UPS device.

Also I have used a bit of the code of @drboss .



The proposed solution reads the status of the UPS device using SNMP protocol and based on this it triggers appropriate actions.

The status of UPS is represented with the various icons of the VD :




Email messages and pop-up notifications are sent whenever :

  •             there is electricity outage
  •           the battery level is critical and HC2 shutdown is triggered
  •         the power is restored

The status is updated whenever the REFRESH button of VD is pressed.

For constant monitoring I have used @Sankotronic’s scene „Main Timer“ which presses the button every minute.






On HC2 -  we need :

-       VD for UPS monitoring and triggering actions

-       Scene to trigger Shutdown of HC2.



We need web server with PHP Module running on something.



It can be RaspberryPI, Windows PC or anything.



In my case I have it on iMac and I followed this guide :




I skipped "Configuring Virtual Hosts" as that was not working for me.

As my web server is accessible only by my devices, I did not care.


Important is that apache web server is managing PHP scripts .



SNMP command line tool has to be present on the server (it is present by default on OS X)


              SNMP tool is present by default with OS X.


              Here are links describing how to install it for Windows  -  different resources (not tested) :






APC UPS with network management card.

The presented solution was implemented and tested with APC Smart UPS 3000  with Network Management Card AP9631






Implement web server with PHP module running.


Activate SNMP command line tool on your server (if not already there).



Copy APC.php script to the web server.

You have to edit the file with any text editor to provide username and password for your HC2, IPs of HC2 and UPS.

If username or password contain `@` it has to be replaced with `%40‘

Eg. If Your username is `[email protected]` You have to enter `user%40`

Note: You have to make a note of the path to the script.


Here is a code :



$password = "Password";                // HC2 password (  '@' has to be replaced with  '%40'  )
$username = "Username";                // HC2 username (  '@' has to be replaced with  '%40'  )
$HC2serverIP = "XXX.XXX.XXX.XXX";      // HC2 IP
$APCIP = "YYY.YYY.YYY.YYY";            // APC UPS IP

// Do not change anything below
function SetGlobalVariable($variable, $value) {
    global $password;
    global $username;
    global $HC2serverIP;
    $url = "http://". $username . ":" . $password . "@" . $HC2serverIP . "/api/globalVariables/" . $variable;
    $data = array('value'=>$value);
    $data_json = json_encode($data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_json)));
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
    $response  = curl_exec($ch);

//$Model = shell_exec('snmpwalk -v 2c -c public -O qv  '".$APCIP."'');
$Model = shell_exec("snmpwalk -v 2c -c public -O qv  '".$APCIP."'");

$Firmware = shell_exec("snmpwalk -v 2c -c public -O qv  '".$APCIP."'");

$UPSInputVoltage = shell_exec("snmpwalk -v 2c -c public -O qv  '".$APCIP."'");

$UPSInputFrequency = shell_exec("snmpwalk -v 2c -c public -O qv  '".$APCIP."'");

$UPSOutputVoltage = shell_exec("snmpwalk -v 2c -c public -O qv  '".$APCIP."'");

$UPSLoadCurrent = shell_exec("snmpwalk -v 2c -c public -O qv  '".$APCIP."'");

$UPSLoad = shell_exec("snmpwalk -v 2c -c public -O qv  '".$APCIP."'");

$UPSTimeOnBattery = shell_exec("snmpwalk -v 2c -c public -O qv  '".$APCIP."'");

$UPSRemainingTimeOnBattery = shell_exec("snmpwalk -v 2c -c public -O qv  '".$APCIP."'");

$UPSRemainingBatteryCapacity= shell_exec("snmpwalk -v 2c -c public -O qv  '".$APCIP."'");

$UPSNeedReplaceBattery = shell_exec("snmpwalk -v 2c -c public -O qv  '".$APCIP."'");

$UPSBatteryReplacedOn = shell_exec("snmpwalk -v 2c -c public -O qv  '".$APCIP."'");

$UPSInternalTemperature = shell_exec("snmpwalk -v 2c -c public -O qv  '".$APCIP."'");

$UPSOutputFrequency = shell_exec("snmpwalk -v 2c -c public -O qv  '".$APCIP."'");

$UPSBatteryVoltage = shell_exec("snmpwalk -v 2c -c public -O qv  '".$APCIP."'");




Create new scene in HC2 for auto shutdown.

%% properties
%% events
%% globals

fibaro:debug("HC2 is shutting down !!!");




IMPORTANT:  You have to pay attention NOT TO CHECK MARK that the scene is starting while HC2 starts.

The HC2 would restart itself after boot.

If you do so you have to do restore to factory state and load last working backup.



Import the VD from the attached .vfib file to your HC2.

Import all provided icons for graphical representation of the UPS status.

There is a bunch of global variables needed, but they will be created automatically by the VD – so you do not have to worry. All of these global variables starts with `UPS_` .



Edit the `USER SETTINGS` section of the LUA code for the REFRESH button :


local IconBatteryGreen	= 1277;		-- enter Icon ID

local IconBatteryRed	= 1278;		-- enter Icon ID

local IconExclMark	= 1279;		-- enter Icon ID

local IconXMark		= 1280;		-- enter Icon ID

local IconPlugged	= 1281;		-- enter Icon ID

local IconTimer		= 1282;		-- enter Icon ID & set this icon for REFRESH Button

local BatteryThresholdRED	= 50;	-- % of Battery Capacity for RED Alert

local BatteryThresholdSHUTDOWN	= 30;	-- % of Battery Capacity for HC2 Shutdown

local PHPScriptPath	= "/PathToPHPScriptOnWebServer/APC.php";	-- path to PHP script on the server

local email		={2}		-- list of email accounts to send notifications - 0 for no notifications

local ShutdownSceneID	= 583;		-- enter Scene ID for HC2 Shutdown




The icon for the REFRESH button should be `IconTimer`.


As the parameters for the VD:

-       enter the IP address of web server with PHP script

-       enter 80 as a port number



That is all.

I hope those how are interested will be able to implement what I did.





What's New in Version JUN-2018   See changelog


No changelog available for this version.

  • Like 1

User Feedback

Recommended Comments

There are no comments to display.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...