Reading power consumption data from Smappee in Loxone

For more info about power consumption metering with Smappee visit http://www.smappee.com

To get this script working you need a webserver running PHP+cURL (I use a Synology NAS; a Raspberry Pi or any other device capable of running a php webserver should be fine).

This PHP script will login to your Smappee and read the current power consumption data:

<?php
$smappee_ip = '192.168.1.188'; // Smappee ip
$smappee_password = 'yourpassword'; // Smappee password (default: admin)

$curl = curl_init();
$result = getData();

if (preg_match("/not authenticated/i", $result) or (empty($result)))
{
  login();
  $result = getData();
}
$output = parseData($result);
curl_close($curl);


function login()
{
global $curl;
global $smappee_ip, $smappee_password;

$url = 'http://'.$smappee_ip.'/gateway/apipublic/logon';
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array('Content-Type:application/json',
'Content-Length: ' . strlen($smappee_password))
);
curl_setopt($curl, CURLOPT_POSTFIELDS, 'admin');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_COOKIEFILE, 'smappee.cookie');
$result = curl_exec($curl);
}

function getData()
{
global $curl;
global $smappee_ip;

$url = 'http://'.$smappee_ip.'/gateway/apipublic/reportInstantaneousValues';
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_COOKIEFILE, 'smappee.cookie');
curl_setopt($curl, CURLOPT_POST, 0);
$result = curl_exec($curl);

return $result;
}

function parseData($data)
{
$data_array = json_decode($data);
$unformatted = $data_array->report;

preg_match("/voltage=(\\d*.\\d)/", $unformatted, $matches);
$volt = $matches[1];

preg_match_all("/ activePower=(\\d*.\\d*)/", $unformatted, $matches);
$p1_watt = $matches[1][0];
$p2_watt = $matches[1][1];
$p3_watt = $matches[1][2];

preg_match_all("/current=(\\d*.\\d*)/", $unformatted, $matches);
$p1_amp = $matches[1][0];
$p2_amp = $matches[1][1];
$p3_amp = $matches[1][2];

preg_match_all("/ cosfi=(\\d*.\\d*)/", $unformatted, $matches);
$p1_cos = $matches[1][0];
$p2_cos = $matches[1][1];
$p3_cos = $matches[1][2];

preg_match_all("/ peak active power (\d*.\d*)/", $unformatted, $matches);
$p1_peak_w = $matches[1][0];
$p2_peak_w = $matches[1][1];
$p3_peak_w = $matches[1][2];

preg_match_all("/ W at (\\d{2}\\/\\d{2}\\/\\d{4} \\d{2}:\\d{2}:\\d{2})/", $unformatted, $matches);
$p1_peak_ts = strtotime($matches[1][0]);
$p2_peak_ts = strtotime($matches[1][1]);
$p3_peak_ts = strtotime($matches[1][2]);

$all_watt = (float)$p1_watt+(float)$p2_watt+(float)$p3_watt;
$all_ampere = (float)$p1_amp+(float)$p2_amp+(float)$p3_amp;
$all_peak = (float)$p1_peak_w+(float)$p2_peak_w+(float)$p3_peak_w;

$all = array("watt"=>$all_watt,"volt"=>$volt,"ampere"=>$all_ampere);
$p1 = array("watt"=>$p1_watt,"volt"=>$volt,"ampere"=>$p1_amp,"cosfi"=>$p1_cos,"peak"=>$p1_peak_w,"peak_ts"=>$p1_peak_ts);
$p2 = array("watt"=>$p2_watt,"volt"=>$volt,"ampere"=>$p2_amp,"cosfi"=>$p2_cos,"peak"=>$p2_peak_w,"peak_ts"=>$p2_peak_ts);
$p3 = array("watt"=>$p3_watt,"volt"=>$volt,"ampere"=>$p3_amp,"cosfi"=>$p3_cos,"peak"=>$p3_peak_w,"peak_ts"=>$p3_peak_ts);

$output = array("all"=>$all,"p1"=>$p1,"p2"=>$p2,"p3"=>$p3);
return json_encode($output);
}
?>

Create a new “Virtual Input” in LoxConfig: “Smappee”. Point URL to the web address of the php script.

smappee_vi

Add a Virtual Input HTTP Command.
Scrape the data from the php script by using command recognition: "all":{"watt":\v

smappee_vci

Eventually connect it to a Utility Meter block to keep nice stats. Since the output value is in Watt and the Utility Meter block expects a value in kWh divide the value by 1000 first.

smappee_utilitymeter

5 thoughts on “Reading power consumption data from Smappee in Loxone”

  1. Hi,
    I was happy to find that script, but unfortunately it returns me always zero. Seems not work, is there no login needed, just the password?

    thx
    hans

    1. I had similar problems after a while and changed the script to verify whether authentication was succesful…works like a charm now (for sourcecode check the original post).

  2. Hi,

    Smappee has also an Gas/Water Monitor.

    Do you have an idea how I have to change your script to get theses data from the gas/water Monitor ?

    Dear regards

Leave a Reply

Your email address will not be published. Required fields are marked *


6 − 3 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>