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

Loxone and Sonos as a doorbell

  • Set up your Sonos device in LoxConfig (Virtual I/O > Predefined devices > Sonos) with the correct IP address. You can retrieve the IP address of your Sonos player from the Sonos application (go to ‘info’ in the Help menu).
  • Add a new virtual output command to the Sonos device, e.g. “Doorbell”
  • Set up these properties:

Command for ON: /MediaRenderer/AVTransport/Control

HTTP extensions for ON:

SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI"

HTTP POST Command:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetAVTransportURI xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><CurrentURI>x-file-cifs://DISKSTATION/music/doorbell.mp3</CurrentURI><CurrentURIMetaData></CurrentURIMetaData></u:SetAVTransportURI></s:Body></s:Envelope>

Replace the path to the doorbell soundfile on your NAS (//DISKSTATION/music/doorbell.mp3 in my case).

HTTP method for on: POST

 

Creating an astro timer in Loxone

This Loxplan configuration scheme allows you to emulate an astro timer with Loxone:

 

loxone_astro

  • Astro sunrise shift : analogue input (slider). If it is set to -30 for example, a pulse will be generated 30 minutes before sunrise.
  • Astro sunset shift : analogue input (slider). If it is set to -30 for example, a pulse will be generated 30 minutes before sunrise.
  • Timer block : just a standard Loxone timer for triggering events indepently from the astro functions.
  • Push-button: this is my manual override for switching on/off the outdoor lights
Skip to toolbar