Control Echo plus devices with shell scripts and routines

I have managed to control my lights through HA that are connected to the hub on an echo plus. The idea came after reading another post . Thanks!

Quite new to this and 100% new to shell (thanks google) so i thought i would right a quick little how to guide and share my scripts hoping it will help someone :slightly_smiling_face:

What it does

Once setup, There will be a service available. It uses a script which allows you to start a routine (and other things) via the shell script. It also saves a log file with the last action.

What it doesn’t
It doesnt allow you to control the brightness or colour (directly) you can turn the devices off and on (although you can set more routines up with brightness colour etc and run them) I have only tried this with lights not with switches, Don’t see why it wouldn’t work though.

How to setup
Quite a few things to setup to get this working;

My script is simple:

#! /bin/sh
filename=$(basename -- "$0")
logdest=/home/homeassistant/scripts/log.txt
timestamp=$(date +%T)
name=$(echo "$filename" | cut -f 1 -d '.')
logoutput="["$timestamp"] "$name" was switched on"
/home/homeassistant/scripts/alexa_remote_control.sh -e automation:$name
echo $logoutput >> $logdest

You can download the alexa script from here
I saved these files in /home/homeassistant/scripts (if you change this you will need to edit my “newlight.sh” or logdest variable with the new path)
Edit the Alexa_remote_controll.sh script with your region & amazon details.
Test it is working by running

sudo ./path/to/alexa_remote_control.sh -a

if everything is ok with the login then it should print your Echo plus or echo dot names.

2 x routines need to be made in the alexa app. One controlling the light to turn on (You can at this point add anything you want to the routine. but i kept it simple) and one to turn it back off again. Name these like mylighton or mylightoff. (The name comes from what ever you type in the “Voice” part when setting up the routine)

Back to the pi again.

sudo cp newlight.sh mylighton.sh

This just makes a copy of my newlight.sh script, My script uses the filename minus the .sh in the command it sends to the alexa_remote_control.sh . So its important its named exactly the way it is in the alexa app on your phone!

Run:

sudo nano /home/homeassistant/.homeassistant/configuration.yaml

And copy platform and the path to your script and script names:

shell_command:
  mylighton: /home/homeassistant/scripts/mylighton.sh
  mylightoff: /home/homeassistant/scripts/mylightoff.sh

On HA check your config and if its ok restart it. Once restarted you should have 2 new services shell_command.mylighton & shell_command.mylightoff if you run these services it will turn your light on and off.

You now have limited control of your Zigbee bulbs attached to alexa! From here you can set them up in scripts within HA to call the new services

Optional:
I also set a switch up to which holds a sort of state for the bulb using input_boolean and allows you to control it from the GUI

input_boolean:
  myroomboolean:
  name: My room light boolean
switch:
  platform: template
  switches:
    myroom:
      friendly_name: My Room
      value_template: >
        {{ is_state('input_boolean.myroomboolean', 'on') }}
      turn_on:
        - service: input_boolean.turn_on
          entity_id: input_boolean.myroomboolean
        - service: script.mylighton
      turn_off:
        - service: input_boolean.turn_off
          entity_id: input_boolean.myroomboolean
        - service: script.mylightoff


Thats it :smiley: If this can be achieved in a better way (less scripts) please let me know. I just hope this helps somebody stuck like i was!

Thanks again to @ReneTode

1 Like

if i am correct then nowadays its also possible to use a service (to start a routine) provided by the (custom) alexa mediaplayer component (also to be found here on the forum).
so you dont need to use the scripts and sh files in that case.

that would probably make your live a bit easier.
in general most lights can be used through other ways then through alexa.
googling for that (python lightype, or controlling lighttype, etc) could make that you can find another way to control them, which would give you full control

I had no idea you could do it through the media player component. I thought it was only for media. Should really read things lol

I’m going to buy a ZigBee adaptor for the pi so everything is connected in the one place(hopefully) The lights I have are hue, osram and hive lights which, to my knowledge, I cant control these any other way except using the Alexa. Before I added them to the echo hub I had full control over them through HA using their respected hubs

if you had full control before then why did you change that?
change back to that way of controling your lights and control HA with alexa, instead of controling alexa with HA.

Because I had 2 hubs already and other stuff I have since bought I wouldn’t be able to use as I don’t have the correct hub. Wanted to try putting them all under one roof. So I got the echo plus added them to that. Used the work around in your other post but they started charging for it. I would just like to add my lights to automations I have set up on my HA and I can do that by the method I posted above.

I’m going to buy a raspbee. Doing a little research on it first.

its a choice, but when you start adding everything to the anazon( alexa) platform, you would lose the need for HA.
but alexa cant control everything you can control with HA, so the best way is to get everything in HA and then control HA with alexa.
so i would put the hubs in working again and the stuff that you cant control with the hubs you mostly can control with HA directly (or maybe you need another hub.)

when not you will probably always have reduced capability with your connected stuff inside HA.

the app from the other post was never used by me and posted by someone else. i always used the shl script. (and have had the media player for a short while)