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!

 

Regards,

Smart Home Forum by FIBARO Team


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

   (1 review)

About This File

PREFACE :

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 .

 

INTRODUCTION :

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 :

Spoiler

a1.png

 

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.

 

 

 

WHAT WE NEED :

1.

On HC2 -  we need :

-       VD for UPS monitoring and triggering actions

-       Scene to trigger Shutdown of HC2.

 

2.

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 :

 

https://medium.com/@JohnFoderaro/how-to-set-up-apache-in-macos-sierra-10-12-bca5a5dfffba 

 

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 .

 

3.

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) :

 

              https://www.ipconvertertools.com/snmpwalkinstructions

              https://syslogwatcher.com/cmd-tools/snmp-walk/

      

4.

APC UPS with network management card.

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

 

 

 

IMPLEMENTATION :

1.

Implement web server with PHP module running.

 

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

 

2.

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 `user@` You have to enter `user%40`

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

 

Here is a code :

 

Spoiler

<?php

//USER SETTINGS
$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
//END OF USER SETTINGS


// 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);
    curl_close($ch);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


?>

 

3.

Create new scene in HC2 for auto shutdown.

Spoiler

--[[
%% properties
%% events
%% globals
--]]


fibaro:debug("HC2 is shutting down !!!");
fibaro:sleep(5000);
HomeCenter.SystemService.shutdown()

 

 

 

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.

 

4.

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 :

Spoiler

--USER SETTINGS  ***

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

--END OF USER SETTINGS  ***

 

 

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.

 

a2.png

 

a3.pnga4.pnga5.pnga6.pnga7.pnga8.png


What's New in Version JUN-2018   See changelog

Released

No changelog available for this version.

  • Like 1


User Feedback

You may only provide a review once you have downloaded the file.


fasq

   1 of 1 member found this review helpful 1 / 1 member

the download only contains the icon. Where is the vfib file?

Link to review
×
×
  • Create New...