In my last post, I created a simple API to control the lights on a PiBrella using NodeRED. I also provided a simple example web page which called that API using Javascript, meaning anyone on my home network can turn my Pibrella lights on and off from a webpage served up from the Raspberry Pi.

I think that’s quite cool. What would be cooler is using voice control 😉

Enter Google Now, Tasker and AutoVoice. If you aren’t familiar with Google Now, it’s the Google equivalent of Siri and is the default launcher in Android Lolliop, and also available for earlier versions of Android. Among other things, it provides the ‘OK Google’ function that lets you perform a Google search or some other action on the device like setting an alarm or reminder using your voice.  Tasker is a paid-for app which allows you to define all kinds of automated commands and activities which can be automatically triggered based on conditions you specify. Finally, AutoVoice is a (paid-for) plug in for Tasker which provides voice integration, including integration with Google Now. You can see where I’m going with this 😉

We’re going to use exactly the same API that we created before, the only difference that instead of calling it from a web page, we’ll trigger Tasker  to make the API call when we speak the appropriate phrase to Google Now.

We’re going to follow the same basic procedure outlined in this Lifehacker post – The first step is to install Google Now (if it isn’t already), Tasker and AutoVoice, and then enable AutoVoice Integration with Google now. To do the latter, go to the device settings page and then the “Accessibilty” page, and look for the “AutoVoice Google Now Integration” option, and make sure it is enabled. You also need to make sure Google Now is configured appropriately, according to the Google Help Page here.

The next step is to create the Tasker profile and voice command which will be used trigger the task to turn the lights on:

  1. Start Tasker and ensure the ‘Profiles’ tab is selected
  2. Click ‘+’ at the bottom of the screen to add a profile
  3. Select ‘Event’
  4. Select ‘Plugin’
  5. Select ‘AutoVoice’
  6. Select ‘Recognized’
  7. You should now have the ‘Event Edit’ screen showing, with ‘Configuration’ displayed. Click the pencil to bring up the ‘AutoVoice Recognized’  configuration panel.
  8. There are now two choices. Either select ‘Command Filter’ and type in the command you want to use, for example “Raspberry lights on”, or
    Select ‘Speak Filter’ and you will be prompted to say the command you want to use.
  9. Once you are happy with the command, select the tick on the top right to save, and return to the (now populated) ‘Event Edit’ screen
  10. Click the ‘Tasker’ icon on the top left to return to the Tasker profiles screen.

At this point we have created the profile and the voice command that will be trigger the profile to be run. However, you cannot save the profile and store it until you have created a task which will be performed when the profile is triggered. We’ll now create some tasks which will call our light switch API, provide feedback to the user, and return to the home screen rather than leave the Google search window up after the command is triggered.

  1. When you return to the ‘Profiles’ tab, you will be prompted to either create a new task, or select an existing task if you have defined one. Choose ‘New Task’ and give it a sensible name.
  2. This takes you to the ‘Task Edit’ page. Click the ‘+’ sign at the end of the page to create a new task, then select ‘Net’ and then ‘HTTP Post’
  3. Add the following:
    Server:Port should be the IP address of your Pi, and the port NodeRED is listening on (default is 1880).
    Path should be /lightswitch (per the URL defined in the previous post)
    Data / File should be {“newlightstate”: “1”}
    Content Type must be set to application/json
  4. Click the ‘Tasker’ icon at the top left to go back to the Task Edit screen, and then add click ‘+’ to add another task
  5. Choose ‘Alert’ and then ‘Say’
  6. In the ‘Text’ field, add something like “Raspberry Pi Lights turned on” and click the ‘Tasker’ icon at the top left to go back to the Task Edit screen
  7. Click ‘+’ again, and this time select ‘App’ then ‘Go Home’. There is no need to customise this further, so click the ‘Tasker’ icon at the top left to go back to the Task Edit screen.

You should now end up with something looking like this:




Return to the Profile list and your voice command should be created. You can now repeat the steps above for switching the lights off and the status check, so ending up with the following:


From the above screen grab, you can see a summary of the commands that will be recognised, and the tasks they will run.

One last item of interest is the status call. For some reason, I had to add to something a little bit fancy, as the screenshot below suggests:


The HTTP Get call to get light status returns some JSON, so the JavaScriptlet is basically a snippet of JavaScript which will convert that JSON to a JavaScript object, and extract the state into a variable called ‘status’ which we can then access in the task. You can see that I have two ‘Say’ actions, and which gets used is dependent on the value of status. One odd behaviour was that without the ‘Wait’ action, the ‘Say’ actions were not triggered. I’m not sure why this is, and I think it’s to do with the way Tasker processes actions. To overcome this, I set the %status variable to a known state, and then wait for it to be changed by the HTTP Get response. There is no error checking here, but you can see the logic.

And that it’s it. I can now say ‘OK Google’ followed by ‘Raspberry Lights On’ to turn my Pibrella LEDs on and get a spoken confirm, then turn them off or check current status using a similar method. And since my tablet is a Nexus 9, I can set Google Now to be always listening and don’t need to physically touch the device at all.