Statistics on my NAS with the help of Fibaro HC2

As beautifull as fibaro home center 2 is with displaying usage statistics, it also lacks the possibility to easily see e.g. weather, temperature and other humidity values or statistics.

This blog contains a way of archiving statistics from the Fibaro Home Center, saving them within a mysql database and presenting them on my own webserver.  Sounds easy, right?

It is actually. The tools used to achieve this are:

  1. NAS Server with the following packages installed/configured
    • PHP
    • MySQL
    • Apache
  2. Fibaro Home Center 2
  3. Forum: http://forum.fibaro.com/viewtopic.php?t=2380

First of all we create a virtual device with 3 buttons:

  1. Temperature & Humidity
  2. Temperature & Weather
  3. Consumption for all supported devices (also includes declared energy values)

Each button gets its own piece of code for uploading the reported values to my NAS.

Uploading temperature & humidity values;
local updatechart = Net.FHttp("172.16.100.100") 
local deviceType = "temperature_sensor" 
local deviceType2 = "humidity_sensor" 
local i = 0 
local maxNodeID = 250
for i = 0, maxNodeID do
 local type = fibaro:getType (i);
 x , y = string.find (type, deviceType) or string.find (type, deviceType2) 
 if x ~= nill then 
   local deviceName = fibaro:getName(i) 
   local deviceValue = fibaro:getValue(i, "value") 
   deviceName = string.gsub(deviceName, "%s+", "%%20");
   payload = "/fibaro-stats/data_post.php?type=" .. type .. "&name=" .. deviceName .. "&value=" .. deviceValue.. "&id=" .. i 
   response, status, errorCode = updatechart:GET(payload) 
 end 
end 
fibaro:log('devices uploaded');
Note: my NAS is located at IP 172.16.100.100. If the number of devices succeed 250, you may need to adjust maxNodeID.
Statistics - Button1
Statistics – Button1
Uploading temperature & weather values;
local updatechart = Net.FHttp("172.16.100.100") 
local deviceName = fibaro:getName(3) 
local data0 = tonumber(fibaro:getValue(3, "Temperature")) 
local data1 = tonumber(fibaro:getValue(3, "Humidity"))

payload = "/fibaro-stats/data_post.php?type=temperature_sensor&name=" .. deviceName .. "&value=".. data0.. "&id=900" 
response, status, errorCode = updatechart:GET(payload) 
payload = "/fibaro-stats/data_post.php?type=humidity_sensor&name=" .. deviceName .. "&value=".. data1.. "&id=901" 
response, status, errorCode = updatechart:GET(payload)

fibaro:log("Weather uploaded");
Note: my NAS is located at IP 172.16.100.100. If the number of devices succeed 250, you may need to adjust maxNodeID.
Statistics - Button2
Statistics – Button2
Uploading consumption for all supported devices;
local updatechart = Net.FHttp("172.16.100.100") 
local deviceUnit = "W" 
local i = 0 
local maxNodeID = 250

for i = 0, maxNodeID do 
  local type = fibaro:get(i, 'unitSensor'); 
  local type2 = fibaro:get(i, 'unit'); 
  local deviceValue = "" 
  x , y = string.find (type, deviceUnit) or string.find (type2, deviceUnit) 

  if x ~= nill or y ~= nill then 
    local deviceName = fibaro:getName(i); 
    if fibaro:getType (i) == "multi_level_sensor" then 
      deviceValue = fibaro:getValue(i, "value") 
    else 
      deviceValue = fibaro:getValue(i, "valueSensor") 
    end 
    deviceName = string.gsub(deviceName, "%s+", "%%20");
    payload = "/fibaro-stats/data_post.php?type=consommation&name=" .. deviceName .. "&value=" .. deviceValue.. "&id=".. i 
    response, status, errorCode = updatechart:GET(payload);
  end 
end 
fibaro:log('Devices uploaded');
Note: my NAS is located at IP 172.16.100.100. If the number of devices succeed 250, you may need to adjust maxNodeID.
Statistics - Button3
Statistics – Button3

Create a scene which runs every minute and launches button 1, 2 and 3:

--[[ 
%% properties 
%% autostart 
%% globals 
--]] 
while true do 
  fibaro:call(61, "pressButton", "1"); 
  fibaro:call(61, "pressButton", "2"); 
  fibaro:call(61, "pressButton", "3"); 
  fibaro:sleep(60000); 
end

Configuring my NAS

Now its time to configure my Qnap NAS. I already have a webserver running. (Almost) every nas has the possibility to become a webserver. So will yours probably.

On your webserver website location (in my case /share/Web) upload and extract the following file: fibaro-stats (all with big thanks to byackee – see forum) in a map called e.g. fibaro-stats.

Create a table (e.g. fibaro-stats) within MySQL and define access rights for a new user. I personally don’t want to interrupt my other databases so I made a seperate user here, called fibaro with the password test (needs to be configured for your own needs).

After this you may import the following file: HighStock (extract first, then import).

Last thing to do is to configure the file config.inc.php with your previous values and you’re set to go!

<?php
$host = "localhost";
$login = "fibaro";
$password = "test";
$table = "fibaro-stats";
?>