Script for syncing devices between Domoticz and EchoBridge

Moderator: leecollings

Post Reply
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Script for syncing devices between Domoticz and EchoBridge

Post by ben53252642 »

This script gets all Domoticz devices, groups / scenes and loads them into EchoBridge for verbal control with Amazon Echo's.

I like to use EchoBridge because I can say "Alexa turn on one" (which is also Bens Bed Lamp) instead of using a skill in which I'd have to say something like "Alexa tell Domoticz to turn on bens bed lamp".
Screen Shot 2016-11-18 at 2.35.00 pm.png
Screen Shot 2016-11-18 at 2.35.00 pm.png (314.96 KiB) Viewed 582 times
https://github.com/armzilla/amazon-echo-ha-bridge

The script is written in PHP, it uses the Domoticz JSON interface and the EchoBridge JSON interface to do the data transfer. This is meant for advanced linux users only.

It automatically configures EchoBridge for each Domoticz device (including dimmable and non-dimmable) and also adds groups / scenes. There is also a section for excluding certain device names such as those containing the word "Motion" or "Sensor".

The script also allows for having additional names for controlling each device (I use numbers in addition to the device names as you can see up the top of the script).

Be sure to configure your paths correctly, this is a custom script and you will absolutely need to configure the path for it to work in your own environment. Incorrectly configuring the rm -rf part of the script may result in serious damage / data loss to your system (as this is the delete command in linux), in this case it is deleting the data directory for EchoBridge so that it can reload it with new synced data from Domoticz.

It is suggested to make a full system backup before running the script.

To run the script, use: php -f SCRIPTNAME.php

In regards to EchoBridge this is the command options I use when running it (I run it in a screen session, the pkill in the script is just searching for my screen session which happens to be named "EchoBridge" and terminating it):

java -jar -Djava.net.preferIPv4Stack=true amazon-echo-bridge-0.4.0.jar --upnp.config.address=192.168.0.5 --emulator.portcount=8

Note the emulator port section which increases the maximum number of devices (to 200) from the default 75 in EchoBridge 0.4.0, this may be required if you have a lot of devices in Domoticz.

Code: Select all

<?php

// Custom device / scene name map
$custom = array(
'Zero'=>'Main Bedroom All Devices',
'One'=>'Bens Bed Lamp',
'Two'=>'Second Bed Lamp',
'Three'=>'Main Bedroom Lava Lamp',
'Four'=>'Main Bedroom LaserPod'
);
// Domoticz Configuration
$server = "USERNAME:[email protected]";

// EchoBridge Configuration
$echobridge = "192.168.0.5:8080";

// Stop EchoBridge
echo "Stopping EchoBridge" ."\n";
shell_exec('pkill -f EchoBridge');

// Remove existing data from EchoBridge
echo "Removing existing data from EchoBridge" ."\n";
shell_exec('rm -rf /root/EchoBridge/data');

// Start EchoBridge
echo "Starting EchoBridge (this may take up to 20 seconds)..." ."\n";
shell_exec('/root/EchoBridge/startup.sh');

// Sleep for 15 seconds while EchoBridge starts
sleep(15);

// Get devices from Domoticz and load them into EchoBridge
$jsonurl="http://$server/json.htm?type=command&param=getlightswitches";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json, true);
foreach ($json_output['result'] as $array){
//    echo $array['Name'] ."\n";
// If device is not dimmable
if (
strpos($array['Name'], 'Motion') !== false ||
strpos($array['Name'], 'Sensor') !== false ||
strpos($array['Name'], 'Toaster') !== false ||
strpos($array['Name'], 'Shredder') !== false ||
strpos($array['Name'], 'Dishwasher') !== false ||
strpos($array['Name'], 'Fridge Door') !== false ||
strpos($array['Name'], 'Bens Mobile Phone') !== false ||
strpos($array['Name'], 'Microwave') !== false ||
strpos($array['Name'], 'Washing Machine / Dryer') !== false ||
strpos($array['Name'], 'Med Sec') !== false ||
strpos($array['Name'], 'Smoke Detector') !== false) {
echo "Skipping sensors";
}else{

foreach ($custom as $key => $value){
if ($array['Name'] == $value) {
$data = [
    'name' => "$key",
    'deviceType' => 'switch',
    'onUrl'   => "http://$server/json.htm?type=command&param=switchlight&idx=$array[idx]&switchcmd=Set%20Level&level=\${intensity.percent}",
    'offUrl' => "http://$server/json.htm?type=command&param=switchlight&idx=$array[idx]&switchcmd=Off",
    'contentType' => 'application/json'];
$data_string = json_encode($data);
$ch = curl_init("http://$echobridge/api/devices");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string)]);
        $response = curl_exec($ch);
print_r($response);
        if(!$response) {
            return true;}
}};

// Import all the remaining named devices from Domoticz into EchoBridge
if ($array['IsDimmer'] == 'true') {
$data = [
    'name' => $array['Name'],
    'deviceType' => 'switch',
    'onUrl'   => "http://$server/json.htm?type=command&param=switchlight&idx=$array[idx]&switchcmd=Set%20Level&level=\${intensity.percent}",
    'offUrl' => "http://$server/json.htm?type=command&param=switchlight&idx=$array[idx]&switchcmd=Off",
    'contentType' => 'application/json'];
}else{
// If device is dimmable
$data = [
    'name' => $array['Name'],
    'deviceType' => 'switch',
    'onUrl'   => "http://$server/json.htm?type=command&param=switchlight&idx=$array[idx]&switchcmd=On",
    'offUrl' => "http://$server/json.htm?type=command&param=switchlight&idx=$array[idx]&switchcmd=Off",
    'contentType' => 'application/json'];
}
$data_string = json_encode($data);
$ch = curl_init("http://$echobridge/api/devices");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string)]);
        $response = curl_exec($ch);
print_r($response);
        if(!$response) {
            return true;}
}};

// Get all groups / scenes from Domoticz and load them into EchoBridge
$jsonurl="http://$server/json.htm?type=scenes";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json, true);
foreach ($json_output['result'] as $array){
$data = [
    'name' => $array['Name'],
    'deviceType' => 'switch',
    'onUrl'   => "http://$server/json.htm?type=command&param=switchscene&idx=$array[idx]&switchcmd=On",
    'offUrl' => "http://$server/json.htm?type=command&param=switchscene&idx=$array[idx]&switchcmd=Off",
    'contentType' => 'application/json'];
$data_string = json_encode($data);
$ch = curl_init("http://$echobridge/api/devices");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string)]);
        $response = curl_exec($ch);
print_r($response);
        if(!$response) {
            return true;}
};
// Process groups again for custom name mappings
$jsonurl="http://$server/json.htm?type=scenes";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json, true);
foreach ($json_output['result'] as $array){
foreach ($custom as $key => $value){
if ($array['Name'] == $value) {
$data = [
    'name' => $key,
    'deviceType' => 'switch',
    'onUrl'   => "http://$server/json.htm?type=command&param=switchscene&idx=$array[idx]&switchcmd=On",
    'offUrl' => "http://$server/json.htm?type=command&param=switchscene&idx=$array[idx]&switchcmd=Off",
    'contentType' => 'application/json'];
$data_string = json_encode($data);
$ch = curl_init("http://$echobridge/api/devices");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string)]);
        $response = curl_exec($ch);
print_r($response);
        if(!$response) {
            return true;}
}}};

?>
Last edited by ben53252642 on Friday 18 November 2016 6:48, edited 1 time in total.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Script for syncing devices between Domoticz and EchoBridge

Post by Egregius »

Nice to see another PHP user :)
Just a small tip for the forum: if you use code=php to start your code block it'll be nicelly formatted ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest