Amazon Echo to Domoticz Bridge: switches, sensors & more

Alexa, Google Home and Siri

Moderator: leecollings

deennoo
Posts: 784
Joined: Wednesday 10 December 2014 13:06
Target OS: Linux
Domoticz version: beta
Location: Bordeaux France
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by deennoo »

blackdog65 wrote:From what I can see there is no immediate plan to roll out into Europe.
There is currently no way to get notifications, even simple reminders are just a sound with no clue as to what you are being reminded of.
No IFTTT, no shopping, no true calendar/to-do integration. Only really ready for US market.

Mine is now boxed up ready for return. I'll wait for Google Home.
Ok thx for your input, will wait for google home too.
Domoticz stable 3.5877 for real & Domoticz beta for test
Rfxtrxe / RFLink / Milight / Yeelight / Tasmota / MQTT / BLE / Zigate
http://domo-attitude.fr
Madgeni
Posts: 1571
Joined: Friday 25 March 2016 17:43
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by Madgeni »

hi -
so the code's been neatened, and now allows control of temperature. I have this working via my Echo, and can set temperature/turn lights & groups On/Off.

https://github.com/madgeni/alexa_domo

Dimming is still a challenge, but that's tied to the Domoticz API, rather than my code (yet!).

Re: Notifications - push notifications are coming to Alexa soon, which *might* allow querying of devices - i'll look at that, when it's made available.
Native Alexa skill and Google app - register at https://www.controlicz.com - https://twitter.com/Controlicz
amz4u2nv
Posts: 31
Joined: Friday 21 August 2015 16:04
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by amz4u2nv »

Madgeni wrote:hi -
so the code's been neatened, and now allows control of temperature. I have this working via my Echo, and can set temperature/turn lights & groups On/Off.

https://github.com/madgeni/alexa_domo

Dimming is still a challenge, but that's tied to the Domoticz API, rather than my code (yet!).

Re: Notifications - push notifications are coming to Alexa soon, which *might* allow querying of devices - i'll look at that, when it's made available.
Hi Madgeni, does this fix the issues with some devices not showing up on the alexa app?
Thanks for your work on this..
Madgeni
Posts: 1571
Joined: Friday 25 March 2016 17:43
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by Madgeni »

Hi -
yes i believe it does - but over to you guys for testing :)
Native Alexa skill and Google app - register at https://www.controlicz.com - https://twitter.com/Controlicz
renerene
Posts: 351
Joined: Wednesday 03 August 2016 11:36
Target OS: -
Domoticz version:
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by renerene »

I've heard that the Echo is coming to the UK and it got my attention when reading about it's possibilities. Gonna keep a close watch on this thread. A connection to Domoticz would be great.
amz4u2nv
Posts: 31
Joined: Friday 21 August 2015 16:04
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by amz4u2nv »

Hey madgeni, how difficult would it be to only include devices that are part of a room so I can filter what Alexa can control. Deleting unnecessary devices from Alexa manually works but they come back again after a while as I assume that Alexa auto discovers frequently.
User avatar
gizmocuz
Posts: 2552
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by gizmocuz »

amz4u2nv wrote:Hey madgeni, how difficult would it be to only include devices that are part of a room so I can filter what Alexa can control. Deleting unnecessary devices from Alexa manually works but they come back again after a while as I assume that Alexa auto discovers frequently.
Thats a great idea, this is what is done for the current homebridge implementation
Quality outlives Quantity!
Madgeni
Posts: 1571
Joined: Friday 25 March 2016 17:43
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by Madgeni »

should be doable, presumably a room has a list of devices in the domoticz.db, which should be accessible via the API somehow - i'll take a look
Native Alexa skill and Google app - register at https://www.controlicz.com - https://twitter.com/Controlicz
amz4u2nv
Posts: 31
Joined: Friday 21 August 2015 16:04
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by amz4u2nv »

Had a go adding the room id -

Code: Select all

function getDevs(context, callback) {

    var headers = {
        namespace: 'Alexa.ConnectedHome.Discovery',
        name: 'DiscoverAppliancesResponse',
        payloadVersion: '2'
    };
    api.getRoomDevices({
        filter: 'all',
        used: true,
        order: 'Name',
	roomid: '3'
    }, function (error, devices) {
        var devArray = devices.results;
        var i = devArray.length;
        lupus(0, devArray.length, function (n) {

            var device = devArray[n];
            var devType = device.type;
            var setswitch = device.switchType;
            i--;
						 
            console.log('device type '+devType + 'and device is '+device);
            if (devType == 0) {
                var appliancename = {
                    applianceId: device.idx,
                    manufacturerName: device.name,
                    modelName: device.name,
                    version: 1,
                    friendlyName: device.name,
                    friendlyDescription: ".",
                    isReachable: true,
                    actions: [
                        "incrementPercentage",
                        "decrementPercentage",
                        "setPercentage",
                        "turnOn",
                        "turnOff"
                    ],
                    additionalApplianceDetails: {
                        switchis: setswitch,
                        WhatAmI: "light"
                    }
                };
                appliances.push(appliancename);
            }
            //else if (devType == 'Temp') {
             //   appliancename = {
             //       applianceId: device.idx,
             //       manufacturerName: device.hardwareName,
             //       modelName: device.subType,
             //       version: device.idx,
             //       friendlyName: device.name,
             //       friendlyDescription: ".",
             //       isReachable: true,
             //       actions: [
             //           "setTargetTemperature"
             //      ],
             //       additionalApplianceDetails: {
             //           WhatAmI: "temp"
             //       }
             //   };
             //   appliances.push(appliancename);
            //}
             else if (devType == '1') {
               var elid = parseInt(device.idx) + 2000;
               appliancename = {
                   applianceId: elid,
                   manufacturerName: device.name,
                   modelName: device.name,
                   version: 1,
                   friendlyName: device.name.replace('[Scene] ',''),
                   friendlyDescription: ".",
                   isReachable: true,
                   actions: [
                       "turnOn",
                       "turnOff"
                   ],
                   additionalApplianceDetails: {
                       WhatAmI: "group"
                   }
               };
               appliances.push(appliancename);
           }
            if (i == 0) {
							console.log ('appliances '+appliances);
                var payloads = {
                    discoveredAppliances: appliances
                };
              //  log(appliances);
                var result = {
                    header: headers,
                    payload: payloads
                };
               callback(result);
            }
        })
    })
}

/**
 * Get all the room devices
 * @param callback
 */
Domoticz.prototype.getRoomDevices = function (_params, callback) {
    var params = {
        filter: 'all',  // Can also be `light`, `weather`, `temperature`, `utility`
        used: true,
        order: 'Name'
    };
    params = extend(params, _params);

    var url = this._getUrl();
    url.addSearch("type", "command");
    url.addSearch("param", "getplandevices");
		

    // Add potential filters
    params["filter"] != undefined && _.isString(params["filter"]) ? _.contains(['all', 'light', 'weather', 'temperature', 'utility'], params["filter"]) ? url.addSearch("filter", params["filter"]) : this : this;
    params["used"] != undefined && _.isBoolean(params["used"]) ? params["used"] ? url.addSearch("used", 'true') : url.addSearch("used", 'false') : this;
    params["order"] != undefined && _.isString(params["order"]) ? url.addSearch("order", params["order"]) : this;
    params["roomid"] != undefined && _.isString(params["roomid"]) ? url.addSearch("idx", params["roomid"]) : this;
    params["roomid"] != undefined && _.isString(params["roomid"]) ? url.addSearch("param", 'getplandevices') : this;
		
    this._request(url, callback);
};


the api returns different values back when going via the room id, so had to change the switch code - Just a hacky attempt, but maybe it will help.
I dont have any temperature setters in the house, so no idea what to put in the switch case.

Cheers,
Amz
amz4u2nv
Posts: 31
Joined: Friday 21 August 2015 16:04
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by amz4u2nv »

this should help out with dimming -
changed in domoticz.js

Code: Select all

Domoticz.prototype.changeSwitchState = function (params, callback) {
    var url = this._getUrl();
    url.addSearch("type", "command");
    url.addSearch("idx", params.idx);
    params.type === 'switch' ? url.addSearch("param", 'switchlight') : this;
  //  params.type === 'dimmable' ? params.state = 'Set Level' : this;
    params.state === 'toggle' ? params.state = 'Toggle' : this;
    params.state === 'on' ? params.state = 'On' : this;
    params.state === 'off' ? params.state = 'Off' : this;
    
		
		if (params.type == 'dimmable')
		{
			url.addSearch("level", parseInt((parseInt(params.state) / 100) * 16));
	    url.addSearch("switchcmd", 'Set Level');
			url.addSearch("param", 'switchlight');		
		}
		else
		{
			url.addSearch("switchcmd", params.state);
		}
		
		console.log('URL called ' +url);
    this._request(url, callback);
};
change in domapi.js

Code: Select all

function handleControl(event, context) {
    var state;
    var idx;

    log("what's the event?", event);

        var accessToken = event.payload.accessToken;
        var what = event.payload.appliance.additionalApplianceDetails.WhatAmI;
        var message_id = event.header.messageId;
        var switchtype = event.payload.appliance.additionalApplianceDetails.switchis;
        var applianceId = event.payload.appliance.applianceId;

        var confirmation;
        var funcName;

        switch (what) {
            case "light":
							var dimmable = 'N';
                if (event.header.name == "TurnOnRequest") {
                    confirmation = "TurnOnConfirmation";
                    funcName = "On";
                }
                else if (event.header.name == "TurnOffRequest") {
                    confirmation = "TurnOffConfirmation";
                    funcName = "Off";
                }
                else if (event.header.name == "SetPercentageRequest") {
                    confirmation = "SetPercentageConfirmation";
										dimmable = 'Y';			
										funcName = event.payload.percentageState.value;
										console.log('percent '+funcName);						
                }
                var headers = {
                    namespace: 'Alexa.ConnectedHome.Control',
                    name: confirmation,
                    payloadVersion: '2',
                    messageId: message_id
                };
								
								if (dimmable == 'N')
								{
                  ctrlLights(applianceId, funcName, function (callback) {
                      var result = {
                          header: headers,
                          payload: callback
                      };
                      context.succeed(result);
                   });
							   }
								 else if (dimmable == 'Y')
								 {
                   ctrlLightsDim(applianceId, funcName, function (callback) {
                       var result = {
                           header: headers,
                           payload: callback
                       };
                       context.succeed(result);
                    });
								 }
								 break;
            case "group":

                var AppID = parseInt(event.payload.appliance.applianceId) - 2000;

                if (event.header.name == "TurnOnRequest") {
                    confirmation = "TurnOnConfirmation";
                    funcName = "On";
                }
                else if (event.header.name == "TurnOffRequest") {
                    confirmation = "TurnOffConfirmation";
                    funcName = "Off";
                }
                 headers = {
                    namespace: 'Alexa.ConnectedHome.Control',
                    name: confirmation,
                    payloadVersion: '2',
                    messageId: message_id
                };
                ctrlScene(AppID, funcName, function (callback) {
                    var result = {
                        header: headers,
                        payload: callback
                    };
                    context.succeed(result);
                }); break;
            case "temp":

                var temp = event.payload.targetTemperature.value;

                applianceId = event.payload.appliance.applianceId;

                if (event.header.name == "SetTargetTemperatureRequest") {
                    confirmation = "SetTargetTemperatureConfirmation";
                    //	flVal = parseFloat(temp);
                }
                headers = {
                    namespace: 'Alexa.ConnectedHome.Control',
                    name: confirmation,
                    payloadVersion: '2',
                    messageId: message_id
                };
                ctrlTemp(applianceId, temp, function (callback) {
                    var result = {
                        header: headers,
                        payload: callback
                    };
                    context.succeed(result);
                });
                break;

            default:
                log("error - not hit a device type");

        }
}

	function ctrlLightsDim(applianceId, func, sendback) {
		console.log('func ' +func);
	    api.changeSwitchState({
	        type: "dimmable",
	        idx: applianceId,
	        state: func
	    }, function (params, callback) {
	        console.log(params, callback);
	        var payloads = {};

	        sendback(payloads)
	    });
	  }	

Madgeni
Posts: 1571
Joined: Friday 25 March 2016 17:43
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by Madgeni »

Thanks for the dimmer bit, that really helped.

I'd done most of the room plan bit, but hopefully neat and efficient - please test it and let me know.

NB - if you want devices discovered now, they need to be in a Room Plan.

https://github.com/madgeni/alexa_domo

release note:
Dimmers and Room Plan control for device discovery now working 18/10
Native Alexa skill and Google app - register at https://www.controlicz.com - https://twitter.com/Controlicz
nigels0
Posts: 221
Joined: Thursday 23 January 2014 12:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by nigels0 »

I've just got myself an Echo Dot in the UK, but rather puzzled as to get this to work - I've cloned the GitHub and opened the readme - but there doesn't seem to be clear step-by-step instructions (or maybe it's just me...). Is there any chance of a wiki?
Madgeni
Posts: 1571
Joined: Friday 25 March 2016 17:43
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by Madgeni »

Hi nigels0 -
is the word doc not clear? Let me know which bit you're struggling with, and i'll help :)
It's basically: Create Smart Home Skill;Create Lambda function for Smart Home - copy in application ID from smart home skill, zip the node_modules folder, domapi.js & package.json, then upload them to the Lambda. Fill in the oauth bit as per the doc in the Smart Home Skill, and test (Basically!).
Native Alexa skill and Google app - register at https://www.controlicz.com - https://twitter.com/Controlicz
User avatar
gizmocuz
Posts: 2552
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by gizmocuz »

@Madgeni, send you a PM a few days ago, i am making a new word document with some more screenshots,instructions, but i am stuck at a certain point
A wiki would be great if the document is approved
Quality outlives Quantity!
nigels0
Posts: 221
Joined: Thursday 23 January 2014 12:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by nigels0 »

Hi Madgeni,

Edit,

well I found the app - you need to link the echo to the developer account - and that meant logging out and then logging back in again. Nonetheless, while I can now access the skill and link to it, I'm not finding any devices to control and yes - I have room plans.

Looking forward to gizmocuz's writeup too - but any help would be appreciated!
Last edited by nigels0 on Saturday 22 October 2016 2:06, edited 4 times in total.
renerene
Posts: 351
Joined: Wednesday 03 August 2016 11:36
Target OS: -
Domoticz version:
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by renerene »

I'm in the Amazon queue for an Echo Dot. Will be installing soon.
So, yes please, a wiki page! Or share the word doc as a Google doc untill then?
nigels0
Posts: 221
Joined: Thursday 23 January 2014 12:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by nigels0 »

Trying out one of the tests - the switch off one - i get an error returned.

the command is:

http://<user>:<password>@<ipaddress>:8080/json.htm?type=command&idx=47&switchcmd=Off

Shouldn't this be:

http://<user>:<password>@<ipaddress>:8080/json.htm?type=command&param=switchlight&idx=47&switchcmd=Off

If there is a problem parsing the json, then maybe that explains why I'm not getting any devices

The discovery test works though.... why am I not getting any appearing as Devices in the app though? Puzzling!
Madgeni
Posts: 1571
Joined: Friday 25 March 2016 17:43
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by Madgeni »

certainly shouldn't be an issue for discovery, but yes, there's a line missing from the light switch statement:

case "light":
//missing the line below;
var switchtype = "switch";
//missing the line above;
if (event.header.name == "TurnOnRequest")



Will also make a start on a comprehensive wiki
Native Alexa skill and Google app - register at https://www.controlicz.com - https://twitter.com/Controlicz
nigels0
Posts: 221
Joined: Thursday 23 January 2014 12:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by nigels0 »

Thought so. By the way, I'm really thankful that you have built this! I'm planning to get the 6-pack of dots and distribute them around the house if the single one works.

Still not clear though about why I'm not getting all devices - maybe I have too many? (I have about 200).


Note that it's probably better to put the Lambda creation before the skill creation as you can get the ARN for the location you want to use (in my case - Ireland) with the correct Lambda function - mine was arn:aws:lambda:eu-west-1:638591532178:function:Domoticz. That was my problem; I blindly copied your ARN which of course was not the correct name of the function. Fixing that, I got 38 devices - exactly the same number that the discovery test code finds.....Still not enough, but a good start.

By adding the missing line in domapi.js, the devices that are found are working now!
nigels0
Posts: 221
Joined: Thursday 23 January 2014 12:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: Amazon Echo to Domoticz Bridge: switches, sensors & more

Post by nigels0 »

amz4u2nv wrote:I am getting the same issues where the devices are not found.
I checked the url used and it brings back whole the devices via the browser.
But for some reason I think via lambda it only brings back a certain amount of devices. I had to make some of my devices and scenes hidden using $ in front of the device name and re-run the alexa discover devices, it then picks up the missing devices.
I can't see anything in the code that would put a limit of how many devices should be discovered. Tried up - ing the timeout and memory allocated, didn't make any effect.
So as a workaround you can hide the devices and incrementally discover them on alexa.
Let me know if you find a better solution.

I have the same issue - seems to discover in units of 19! Painful to have to do the same thing with a couple of hundred devices! Not sure whether it is a Domoticz problem or an a Lambda problem - I suspect it is Lambda as Domoticz does report all the devices from the browser using the same JSON.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest