You could also do this with the automation component instead so whenever you put your house to sleep mode for example your Android device will open up Google Play Books or the Kindle app ready for you to read as well as dimming your lights, but this tutorial is all about the switches.
First things first you should install Tasker and AutoRemote onto your Android device and launch AutoRemote. You should see a URL above the QR code, visit it in your browser and it should bring up a page a bit like this.
Now type in
SayOn in the
Message box and you should see a box appear on the right with a URL in it, this is what we will be using in the Python script later on so save that for later. Do the same thing again but this time replace
SayOff. Now just click the
Send message now! button to test that your commands will get sent to your Android device, if they do you will see a toast message at the bottom of your screen like this one.
Open up Tasker and make sure you’re in the
PROFILES tab, then select the plus icon to create a new profile. Select
AutoRemote and then the pencil icon to configure the AutoRemote event. Select
Message Filter and enter in
SayOn then go back until it asks you for a task. Select
New task then just leave the next field blank and select the tick icon.
This is where we’ll configure our task, so select the plus icon to select an action. Select
Say to add a Say action. Enter
On in the text field and go back to test your task, make sure your media volume is up then select the play icon, you should hear your device say “On”.
Now you can go back to the main Tasker screen and create another profile but this time replace
Off. After you’ve done that go to the main screen again and select the menu button at the top then
Save first to make sure everything is saved properly.
Now it’s time to set it up the script, so create a new Python script and name it
On.py then enter this code:
Enter in your “On” URL then save it. Create another script but this time call it
Off.py and enter your “off” URL instead.
Add a command line switch to your Home Assistant configuration:
oncmd: python "[LocationOfOnScript]"
offcmd: python "[LocationOfOffScript]"
Now load up Home Assistant and whenever you toggle the switch you created your Android device will respond with either “On” or “Off”. :-)