Adding a Plugwise Smile to the Fibaro HC2

Yep, I’m still alive 😉 The ‘silence’ since April this year was simply because I really needed some sort of ‘sabbatical’ after almost 10 years of spending a great deal of my spare time to Home Automation – and some other (more important) things happened in the past months as well. But last week something happened that brought back the inspiration to write a new post.

I think it must have been about 2 weeks ago when Marco from Mood LedLight contacted me to help him to get some information the Plugwise Smile can provide into his Fibaro HC2. He was probably inspired by my post about getting Smart meter data into a HC2. And Marco is the one that advised me on my Gazebo LED project some years ago and he’s my LED strip supplier. I don’t own a Plugwise Smile but thought I’d give it a try – if it can be done I should be able to do it, right? 😉

Marco sent me some info about the progress he had already made – he used a PHP script to query the Smile and write the result to a file on a Windows 2008 IIS server and read that file from a lua script in a Fibaro virtual device and actually wanted to get rid of the PHP part.

The only part I did was getting the authentication working so that it was possible to get the Smile XML data without the use of the Win2k8 Web server. For the last part, XML parsing in Lua, I didn’t have enough time so I asked Marco to seek for help on that from the real Fibaro Lua script cracks – cause I only know the basics. 3 days later Marco had found the solution; here it is… have fun!

--[[
%% properties
%% globals
--]]
 
-- Virtual device id
vDevId = 102
 
-- url van PI met meterconnectie
PI1 = Net.FHttp("192.168.xx.xx");
PI1:setBasicAuthentication("smile", "fake");
 
-- ophalen JSON info
response, status, errorCode = PI1:GET("/core/modules");
--fibaro:debug(response);
 
-- Meter info ophalen 
--local a = string.match(response, "^.+<vendor_name>(.+)</vendor_name>.+$");
--local b = string.match(response, "^.+<modified_date>(.+)</modified_date>.+$");
--local c = string.match(response, "^.+<created_date>(.+)</created_date>.+$");
--local d = string.match(response, "^.+<vendor_model>(.+)</vendor_model>.+$");
 
-- Uitlezen meters
local e = string.match(response, "unit='W' directionality='consumed'>(.+)</measurement>.+$");
local e1, e2, e3, e4, e5, e6, e7, e8, e9 = string.match(e, "(%x.+)%s(%x.+)%s(%x.+)%s(%x.+)%s(%x.+)%s(%x.+)%s(%x.+)%s(%x.+)%s(%x.+)");
 
-- omzetten naar decimalen
local e1 = string.match(e1, '%d+')
local e2 = string.match(e2, '%d+')
local e3 = string.match(e3, '%d+')
local e4 = string.match(e4, '%d+')
local e5 = string.match(e5, '%d+')
local e6 = string.match(e6, '%d+')
local e7 = string.match(e7, '%d+')
local e8 = string.match(e8, '%d+')
local e9 = string.match(e9, '%d+')
 
-- Totaal tellers (dag+nacht-teruglevering)
local e10 = e8 + e9 - e6
 
-- delen door 1000 voor KwH
local e6 = e6/1000
local e8 = e8/1000
local e9 = e9/1000
local e10 = e10/1000
 
-- afronden op 2 decimalen
local e6 = string.format("%.2f", e6)
local e8 = string.format("%.2f", e8)
local e9 = string.format("%.2f", e9)
local e10 = string.format("%.2f", e10)
 
-- waardes weergeven
--fibaro:debug("vendor name :" .. a);
--fibaro:debug("vendor_model :" .. d);
--fibaro:debug("modified date :" .. b);
--fibaro:debug("created date :" .. c);
fibaro:debug(" Huidig gebruik : " .. e1 .." Watt" );
--fibaro:debug(" waarde 2 : " .. e2 );
--fibaro:debug(" waarde 3 : " .. e3 );
--fibaro:debug(" waarde 4 : " .. e4 );
--fibaro:debug(" Nachtteller totaal : " .. e5 .." KiloWatt uur");
--fibaro:debug(" Dagteller totaal : " .. e6 .." KiloWatt uur");
--fibaro:debug(" Totaal tellers : " .. e7 .." kiloWatt uur");
--fibaro:debug(" waarde 5 : " .. e5 );
fibaro:debug(" Terug Levering totaal : " .. e6 .." KwH");
--fibaro:debug(" waarde 7 : " .. e7 );
fibaro:debug(" Nachtteller totaal : " .. e8 .." KwH" );
fibaro:debug(" Dagteller totaal : " .. e9 .." KwH" );
fibaro:debug(" Totaal tellers : ".. e10 .." KwH" );
 
-- Update virtual device
fibaro:call(vDevId, "setProperty", "ui.lbldagverbruik.value", e9.." KWh");
fibaro:call(vDevId, "setProperty", "ui.lblnachtverbruik.value", e8.." KWh");
fibaro:call(vDevId, "setProperty", "ui.lblactueelverbruik.value", e1.." Watt");
fibaro:call(vDevId, "setProperty", "ui.lbltotaalverbruik.value", e10.." KWh");
 
-- Temp dag start ophalen en aftrekken van totaal om dag totaal te berekenen
local myVar1 = fibaro:getGlobalValue('Watt_today_start');
fibaro:debug ("waarde van de variable dag start is ".. myVar1);
local myVar2 = fibaro:getGlobalValue('Watt_Temp');
fibaro:debug ("waarde van de variable dag totaal is ".. myVar2);
local myVar3 = (myVar2 - myVar1);
local myVar3 = string.format("%.2f", myVar3)
fibaro:debug ("waarde van de variable dag gebruik is ".. myVar3);
fibaro:call(vDevId, "setProperty", "ui.lbltodayverbruik.value", myVar3.." KWh");
 
-- Gisteren
local myVar4 = fibaro:getGlobalValue('Watt_yesterday');
fibaro:call(vDevId, "setProperty", "ui.lblyesterdayverbruik.value", myVar4.." KWh");
fibaro:debug ("waarde van de variable gisteren gebruik is ".. myVar4);
 
-- update global variable
fibaro:setGlobal("Watt_current", e1);
fibaro:setGlobal("Watt_Temp", e10);
fibaro:setGlobal("Watt_today", myVar3);
 
-- ff 20 seconden slapen
fibaro:sleep(20*1000)