Montag, 20. September 2021

Alexa Remote Control Shell Script

 It's about time for a new post since a lot of things have changed over the course of the past four years.

First things first though, the good news is there's finally an almost official way to properly use the script with your Amazon Alexa. Luckily all major home automation projects became inspired by the idea of the original script and some inner workings have been discovered by people smarter than me. Michael Geramb of openHAB and Ingo Fischer of ioBroker came up with the idea to proxy the login to Amazon very similar to how a mobile app would.

The result was for example the nodeJs package alexa-cookie2. Looking more closely at the result of the login process it became clear that only a single item is required for our purposes. Hence I created a small nodeJS app that will output the token after successfully completing the login.

As not everybody would want to install nodeJS (or run it in Docker) I created packaged binaries using the nodeJS packager pkg.

While the old login flow may still work (for some time) I strongly encourage everybody to try the new cleaner method.

You will have to do this only once - very likely not on the actual machine that is going to run the alexa_remote_control.sh script, but on your browser enabled workstation!

  1. Start the alexa-cookie-cli-<linux|macos|windows> on your workstation from the command-line
    (cli is important as the window might close otherwise!!!)
  2. Point your browser at http://localhost:8080/
  3. Login to Amazon
  4. Upon successful login the program will exit and return the refresh_token to the command-line output (something like Atnr|...)

This will be the value of the REFRESH_TOKEN environment variable in the alexa_remote_control.sh script. No need to store your precious password anymore!

There are some options that alter the behavior if you are not an amazon.de user. Run the binary with --help to see.

Quite a few changes might have gone unnoticed. One of the most important is the use of environment variables. While you can always edit the script (as I originally intended) and modify the settings there, every SET_ variable has it's counterpart in an environment variable (without the SET_ prefix). 

I am going to demonstrate that behavior by using a feature which also seems not to be well understood. There is individual volume level control by Echo device names. Assume you have three Echos:

DEVICEVOLNAME='Kitchen LivingRoom BedRoom'

and you want the TTS volumes to be different:

DEVICEVOLSPEAK='100 80 25'
(the list of volumes refers to each of the Echo devices)

By creating a wrapper script around alexa_remote_control.sh you could even do something like:

#!/bin/sh
# alexa.sh wrapper script
#
export REFRESH_TOKEN='Atnr|...'
export DEVICEVOLNAME='Kitchen LivingRoom BedRoom'

# hush, hush during evening/night time
H=$(date +%H)
if [ $H -ge 19 -o $H -le 7 ] ; then
        export DEVICEVOLSPEAK="80 50 12"
        export DEVICEVOLNORMAL="70 40 9"
else
        export DEVICEVOLSPEAK="100 80 25"
        export DEVICEVOLNORMAL="80 50 12"
fi

/some/path/alexa_remote_control.sh "$@"

2 Kommentare:

  1. Awesome! Gotta try it out asap, thanks for continuous work you put into this :)

    AntwortenLöschen
  2. Hello,
    I would like to use alexa_remote_control.sh in a Rpi4 working in headless mode.

    How can I get the token? Please help!

    Thanks a lot,
    Dan

    AntwortenLöschen