Alexicz - FREE Domoticz to Alexa Integration

Alexa, Google Home and Siri

Moderator: leecollings

eserero
Posts: 17
Joined: Monday 25 June 2018 23:41
Target OS: Windows
Domoticz version:
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by eserero »

Do you know me what parameter to add to the Alexa endpoint will be included in an Alexa group?
I searched a bit, and couldn't find a documentation that explain how groups works in smart home skill API.
User avatar
philchillbill
Posts: 396
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by philchillbill »

There isn’t one. The groups in the app have nothing to do with the discovery process. You are better off playing with groups in Domoticz if you want discovery to handle things. A group and scene are very similar. The main difference is that you cannot turn off a scene but you can turn off a group. And groups switch in principle all together while scene device can take their time to adjust.


Sent from my iPhone using Tapatalk
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
eserero
Posts: 17
Joined: Monday 25 June 2018 23:41
Target OS: Windows
Domoticz version:
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by eserero »

hmmm- I think I wasnt clear. I dont want to use the domotics group/scene concept. I would like to use the domoticz roomplan and create alexa groups for rooms - so this means all the lights of my living room, and the temperature sensor, and the window sensor and the air conditioner switch will all be under one room in alexa.

this allows me to say something like "Alexa, turn on the light in the bedroom" and alexa will turn on all the lights in the bedroom.
it also allow me to add my amazon echo to the alexa group (in my case "bedroom" group), and then I can say " Alexa, turn on the lights" and it will turn only the lights in the room. this actually work very nicely if I create the groups from alexa app.
what i'm looking for is a way to sync the domoticz room plan to the alexa groups automatially.
User avatar
philchillbill
Posts: 396
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by philchillbill »

You were clear and I answered that - there’s no link between discovery and the app groups. You have to do it manually.


Sent from my iPhone using Tapatalk
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
Damsee
Posts: 58
Joined: Thursday 20 April 2017 1:17
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by Damsee »

To complete philchillbill comments, you have 2 options to turn on all lights in your bedroom :

1. Create a DZ group will all bedroom lights + set option in "configdz.json" for your skill "includeScenesGroups": true
Alexa will create a group from DZ during the discovery

2. Create manually a group in Alexa (alexa.amazon.com) >> Smart Home >> Groups >> Create Group >> Devices Group...
You can therefore select manually all your bedroom lights and create a group to control them.
Note : You don't have to create group/scene within DZ with that 2nd option...

Hope that helps ;)
RPI3 Raspbian Buster + Domoticz v2020.1 stable + RFXtrx433E + Z-Stick Gen5 + Amazon Echo (alexicz)
x5 THGN810 / x5 ZMNHJD1 / x2 Flood sensor FGFS101 / x1 Smoke sensor FGSD002
x1 ZMNHAD1 / x2 FGS213 / x3 Wallplug FGWPE/F / NodOn Soft remote CRC-3-6-0
eserero
Posts: 17
Joined: Monday 25 June 2018 23:41
Target OS: Windows
Domoticz version:
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by eserero »

Thanks Damsee, Philchillbill - this helped.
I went for the manual option, the nice thing is that it lets you refer to a room, and not to a device name, so you can ask - whats the temperature in this room, turn on lights in this room etc.
Another nice thing is that you can include the amazon echo device in the room, then you just need to say "turn on the lights".

This integration works Brilliantly! only one skill for everything, and except Amazon (necessary evil :-)) I dont need to share my data with anyone.
also - response time is quite decent.

I did searched the forum a bit and added "no_alexa" support as well as using "Alexa_Name" - very useful to filter out what you need.

BTW - any progress on support for switches with states? I saw there is initial support, can you explain how I can try to use it? (what do I need to put in the description)
mcwieger
Posts: 107
Joined: Tuesday 08 December 2015 9:59
Target OS: NAS (Synology & others)
Domoticz version: 2020.1
Location: Netherlands
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by mcwieger »

Eserero's comments triggered me, as I did not use rooms in Alexa before. So I did the following:

- Created group in Alexa
- Added lights and applicable Echo Dot in the group
- Asked Alexa to turn on the lights

A response was given, but nothing happened. My guess is because none of my lights are recognized as lights in Alexa. In the web interface they show as 'Light/Switch' and in the app as 'Switch'.

What makes a device a Light or a Switch in Alexa? Is this a setting in Domoticz or Alexicz?
I moved to HomeAssistant. It was fun while it lasted!
User avatar
philchillbill
Posts: 396
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by philchillbill »

In DomoticzHandler.py, in the discovery code you will see "endpoint.addDisplayCategories('SWITCH')" and "endpoint.addDisplayCategories('LIGHT')" statements in the code. These add the relevant - you guessed it - display categories. I've totally regrouped the whole discovery code to suit my needs so I can't just paste a sample, but if you play around with these statements you can achieve wonders.
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
draghip
Posts: 6
Joined: Thursday 21 November 2019 13:39
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: Italy
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by draghip »

Hello, I am new to this forum, I have Domoticz running at home on a Raspberry Pi3 since a few years. Temperature sensors, blinds control, some relays. Now I would like to integrate it into Alexa, but I am not able to get it running. I tried the JSON scripts suggested in the manual and they are working, but the lambda scripts show some errors I cannot understand. Is there a way to debug this issue step by step? Can someone help me (in private is also fine)?
Many thanks in advance
ld57
Posts: 11
Joined: Sunday 31 July 2016 22:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by ld57 »

draghip wrote: Thursday 21 November 2019 14:15 but the lambda scripts show some errors I cannot understand. Is there a way to debug this issue step by step?
Yes just post your logs here! ;)

BTW many thanks to the author! I got lots of problem with my Echo in France, and my dev+amazon account in US, but after uploading the lambda to both EU and US, and switching my account to FR it finally worked. (except for an X10 virtual switch, I'll post more logs later).
Domoticz + RPI3B+ + Zwave devices + RFLink
My blog: https://drolez.com/blog/
traolo
Posts: 13
Joined: Friday 22 November 2019 20:40
Target OS: -
Domoticz version:
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by traolo »

Hello,
thanks for this great work!
I was wondering if it is possible to create a routine in the Amazon app based on the status of a contact sensor and make it work. I can import the sensor in the Alexa app, and create the routine. The status of the sensor is reflected in the Alexa app but the routine is not triggered. I'm using the dev-phil scripts.
User avatar
philchillbill
Posts: 396
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by philchillbill »

To trigger the routine you need external software that sends an async State change report to the Alexa gateway. I’ll see if I can document what I did myself and post it.


Sent from my iPhone using Tapatalk
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
traolo
Posts: 13
Joined: Friday 22 November 2019 20:40
Target OS: -
Domoticz version:
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by traolo »

philchillbill wrote: Friday 22 November 2019 21:40 To trigger the routine you need external software that sends an async State change report to the Alexa gateway. I’ll see if I can document what I did myself and post it.


Sent from my iPhone using Tapatalk
Hello, thanks for your answer. It is more clear now.
So did you find a way to get the authentication token from the lambda logs? Do you have a sample of the message you send no notify the alexa gateway the change status of the sensors? I was thinking a static script triggered by the start/stop action of each contact and motion sensor would be a good solution for now.
User avatar
philchillbill
Posts: 396
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Alexicz - FREE Domoticz to Alexa Integration

Post by philchillbill »

OK, here we go. This is what's needed to push Domoticz state changes for door/window contacts to be usable in Alexa routines. Make sure you have the https://github.com/rimram31/dz_smarthome/tree/dev dev branch of Alexicz from Github which is where work on async reporting started by RimRam31. Note you will need to re-do account linking for Alexicz when all done.

First, go to the Alexa Developer Console and under the 'Build' tab for Alexicz, go to the permisisons section and select 'Send Alexa Events'. Note that when you do that, account linking will always fail unless you properly implement the AcceptGrant response (next section). So if you do anything wrong later on and cannot link your account, go back to this portal and turn this option back off again. That's important to remember !

Take note of the Alexa Client ID and Alexa Client secret on this permission page. And remember, they are different to the LWA client ID/secret used for account linking itself.

Add the following class to the AlexaSmartHome.py file to respond to the AcceptGrant directive which is sent to the skill when account linking completes with event permissions. I added it just under class Discovery(AlexaSmartHomeCall) inside class Alexa(object).

Code: Select all

    class Authorization(AlexaSmartHomeCall):

        def AcceptGrant(self, directive):
            print('AcceptGrant: ', directive)
            try:
                if 'grantee' in directive['payload']:
                    self.handler.setAccessToken(directive['payload']['grantee']['token'])
            except Exception: pass
            response = {
                'event': {
                    'header': {
                        'namespace': 'Alexa.Authorization',
                        'name': 'AcceptGrant.Response',
                        'payloadVersion': '3',
                        'messageId': str(uuid4())
                    },
                    'payload': {}
                }
            }
            return response
Now, when you account link the skill, the CloudWatch logs will show an entry with the AcceptGrant request and the payload will contain a short code that looks something like RHwmsruJWOeBfqHdFEfN. Note that it's only valid for about 5 minutes and you get one shot with it - if you make any mistakes later on, you'll have to disable the skill, redo account linking, get a new code. Rinse and repeat. We will exchange that short code for the much longer Atza|blah-blah codes that are needed for the async change reports using a script called from Domoticz.

Upload that modified AlexaSmartHome.py file to the Lambda function for Alexicz.

Now create a totally new python file called reportstate.py which will be run on the machine your Domoticz is on and NOT in the Lambda. This is the script you will execute from the on/off script actions - with the IDX of the triggering device - to send the events from Domoticz to the Alexa gateway.

EDIT: THERE'S A BETTER VERSION OF THIS SCRIPT A FEW POSTS DOWN !

Code: Select all

#!/usr/bin/env python3
# (c) philchillbill, 2019. No rights reserved.

import sys, time, datetime, json, uuid, os
import requests
import AlexaSmartHome, DomoticzHandler

ALEXA_URI = "https://api.eu.amazonalexa.com/v3/events"
CODE = "RHwmsruJWOeBfqHdFEfN"
CLIENT_ID = "the.client.id.from.the.permissions.for.send.alexa.events" 
CLIENT_SECRET = "the.client.secret.from.the.permissions.for.send.alexa.events"
REDIRECT_URI =  "https://www.amazon.com/ap/oa/?redirect_url=https://layla.amazon.com/api/skill/link/xxxxxxxxxxxxxxxxxx"
LOCAL_DOMOTICZ_IP = "http://192.168.178.12:8080"

PREEMPTIVE_REFRESH_TTL_IN_SECONDS = 300
PATH = os.path.dirname(os.path.abspath(__file__))
TOKEN_FILENAME = CODE + ".txt"
UTC_FORMAT = "%Y-%m-%dT%H:%M:%S.00Z"
LWA_TOKEN_URI = "https://api.amazon.com/auth/o2/token"
LWA_HEADERS = { "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8" }

def get_utc_timestamp(seconds=None):
    return time.strftime(UTC_FORMAT, time.gmtime(seconds))

def get_utc_timestamp_from_string(string):
    return datetime.datetime.strptime(string, UTC_FORMAT)

def get_uuid():
    return str(uuid.uuid4())

def get_need_new_token():
    """Checks whether the access token is missing or needed to be refreshed"""
    need_new_token_response = {
        "need_new_token": False,
        "access_token": "",
        "refresh_token": ""
    }

    if os.path.isfile(TOKEN_FILENAME):
        with open(TOKEN_FILENAME, 'r') as infile:
            last_line = infile.readlines()[-1]
        token = last_line.split("***")
        parts = len(token)
        # sometimes we get extra or zero LFs in the last line so compensate
        if parts == 3:
            real_last_line = last_line.split("}")[1] + "}"
            token = real_last_line.split("***")
        token_received_datetime = get_utc_timestamp_from_string(token[0])
        token_json = json.loads(token[1])
        token_expires_in = token_json["expires_in"] - PREEMPTIVE_REFRESH_TTL_IN_SECONDS
        token_expires_datetime = token_received_datetime + datetime.timedelta(seconds=token_expires_in)
        current_datetime = datetime.datetime.utcnow()

        need_new_token_response["need_new_token"] = current_datetime > token_expires_datetime
        need_new_token_response["access_token"] = token_json["access_token"]
        need_new_token_response["refresh_token"] = token_json["refresh_token"]
    else:
        need_new_token_response["need_new_token"] = True

    return need_new_token_response

def get_access_token():
    """Performs access token or token refresh request as needed and returns valid access token"""

    need_new_token_response = get_need_new_token()
    access_token = ""

    if need_new_token_response["need_new_token"]:
        if os.path.isfile(TOKEN_FILENAME):
            with open(TOKEN_FILENAME, 'a') as outfile:
                outfile.write("\n")

            lwa_params = {
                "grant_type" : "refresh_token",
                "refresh_token": need_new_token_response["refresh_token"],
                "client_id": CLIENT_ID,
                "client_secret": CLIENT_SECRET,
                "redirect_uri": REDIRECT_URI
            }
        else:
            lwa_params = {
                "grant_type" : "authorization_code",
                "code": CODE,
                "client_id": CLIENT_ID,
                "client_secret": CLIENT_SECRET,
                "redirect_uri": REDIRECT_URI
            }

        response = requests.post(LWA_TOKEN_URI, headers=LWA_HEADERS, data=lwa_params, allow_redirects=True)

        if response.status_code != 200:
            return None

        token = get_utc_timestamp() + "***" + response.text
        with open(TOKEN_FILENAME, 'a') as outfile:
            outfile.write(token)

        access_token = json.loads(response.text)["access_token"]
    else:
        access_token = need_new_token_response["access_token"]

    return access_token


def main(argv):
    token = get_access_token()
    device_id = argv[0]
    portal = DomoticzHandler.Domoticz(LOCAL_DOMOTICZ_IP)
    
    if device_id != '':
        device = portal.getDevice(device_id)
        endpoint = portal.getEndpointDevice(device)     
        data = AlexaSmartHome.api_reportState(endpoint, token)
        headers = { 'Content-Type': 'application/json' }
        headers['Authorization'] = 'Bearer ' + token
        response = requests.post(ALEXA_URI, headers=headers, json=data)
        print(response)
        

if __name__ == "__main__":
    main(sys.argv[1:])
You will need to edit the parameters at the top of the file to match your setup:

ALEXA_URI = "https://api.eu.amazonalexa.com/v3/events"
CODE = the short code you get from the AcceptGrant directive above
CLIENT_ID = from Developer Portal
CLIENT_SECRET = from Developer Portal
REDIRECT_URI = from Developer Portal, under account linking
LOCAL_DOMOTICZ_IP = your local Domoticz instance, with port number

Make sure to place a copy of the AlexaSmartHome.py and DomoticzHandler.py files in the same directory as this script so it can import them.

Test that it works by doing chmod +x and typing e.g. ./reportstate.py 379 (where 379 is the Domoticz IDX of a door/window sensor). That sensor will now be usable in a routine in the Alexa app. If all is ok, trigger it from now on with "e.g. script://reportstate.py 123" in your Domoticz devices script fields.

Enjoy. If anybody feels like making this a plugin for Domoticz, be my guest !
Last edited by philchillbill on Tuesday 26 November 2019 21:39, edited 3 times in total.
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
traolo
Posts: 13
Joined: Friday 22 November 2019 20:40
Target OS: -
Domoticz version:
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by traolo »

Thank you phil,
I did almost all, but facing an issue when testing the reportstate.py script.
I got the error:
File "./reportstate.py", line 108, in main
portal = DomoticzHandler.DeviceCloud(LOCAL_DOMOTICZ_IP)
AttributeError: module 'DomoticzHandler' has no attribute 'DeviceCloud'

Looking at the DomoticzHandler script, the DeviceCloud class is missing... Am I wrong?
User avatar
philchillbill
Posts: 396
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by philchillbill »

Sorry - my bad. I've renamed many things in my own copy of Alexicz and didn't spot that one when I tried to match my script to the online Alexicz version. The correct class name is Domoticz and not DeviceCloud. I've edited the posting above to correct it. Hope it works now for you !
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
traolo
Posts: 13
Joined: Friday 22 November 2019 20:40
Target OS: -
Domoticz version:
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by traolo »

Yes I was looking and found the right class name, but I think you developed a method for getEndpointDevice. I see only the getEndpoints method
User avatar
philchillbill
Posts: 396
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by philchillbill »

Are you sure you have RimRam's dev branch instead of dev-phil? My getEndpoints is called getEndpointDevice in RimRam's file. I based the description above on his version as it had the async code without all my renaming.
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
traolo
Posts: 13
Joined: Friday 22 November 2019 20:40
Target OS: -
Domoticz version:
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by traolo »

Got it,
you linked the master branch instead of the dev one. I will test and let you know. Thank you :-)
User avatar
philchillbill
Posts: 396
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Alexicz - FREE Domoticz to Alexa Integration

Post by philchillbill »

Oh boy. One of those days. Sorry. Does it work now?

Edit: I see the dev branch in the original post. Not sure why you think I linked the master.
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest