I have received a few inquiries about the AutoVoice regex groups. I decided to put together a very quick tutorial on how to approach the commands like this. When is it useful? Each time you wish to have a voice command which can perform different tasks based on the words used in your sentence. For the sake of the example, let’s assume I have super-futuristic light control in my house. I can enable or disable light in each room, or turn them all off etc. The easy way of doing it is to have your command spoken and to have dozens of variants of your commands listed as profiles in AutoVoice. This would work, but the process is lengthy, tedious and there are better ways of doing it.
I WANT TO TURN ON A BLUE LIGHT!
AutoVoice regex groups allow you to assign part of the voice command to a custom variable. We already have an array for each word, however, this way you will be able to be more precise. The full version of this command, with all available options, would be:
I WANT TO TURN ON A BLUE LIGHT!
The command filter in the AutoVoice allows us to easily type this all in, however, we need to add AutoVoice regex groups to capture the words we need, and assign it to custom variables.
I WANT TO TURN (?<STATE>ON|OFF) (?<COLOUR>BLUE|RED|ALL) LIGHT
How to translate this:
(?<name_your_variable_here>enter|possible|options)
or(?<name_your_variable_here>.+)
if you are not sure what your option will be, and you wish to capture one or more character.
In my example, I’m using the preset values, as I know what lights I will be turning on and off. Also, the .+ option should be used only at the end of the command, or put at least one word in between capturing groups. Otherwise, you are risking conflicts.
How about different command structure?
Actually, there isn’t any harm in using the AutoVoice regex groups for this. You will end up with few capturing groups that are not used, but all will work just fine. Single words can be also easily separated by the | symbol.
(?<COMMAND>PLEASE|I WANT TO) TURN (?<STATE>ON|OFF) (?<COLOUR>BLUE|RED|ALL) LIGHT|LIGHTS
How Tasker is set:
Here is the description of the task:
Task: Test Abort Existing Task A1: Stop [ With Error:Off Task: ] If [ %colour !Set & %state !Set ] A2: Variable Set [ Name:%State_led To:'%state' Do Maths:Off Append:Off ] A3: Variable Set [ Name:%Colour_led To:'%colour' Do Maths:Off Append:Off ] A4: AutoTools SSH [ Configuration:Server: 192.168.0.3 Port: 32 Username: pi Password: ******* Command: python /home/pi/Documents/My\ Scripts/led.py %Colour_led %State_led Command Variable: atsshresult Timeout (Seconds):60 ] A5: Say [ Text:Ok the %colour will get turned %state Engine:Voice:default:default Stream:3 Pitch:5 Speed:5 Respect Audio Focus:On Network:Off Continue Task Immediately:Off ]
And the Raspberry?
If you must – the set up is rather simple, as it was just to show the effect of the RegEx visually.
import RPi.GPIO as GPIO import sys GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) #Blue LED GPIO.setup(26,GPIO.OUT) #Red LED GPIO.setup(21,GPIO.OUT) def lights_on(pin): GPIO.output(pin,GPIO.HIGH) def lights_off(pin): GPIO.output(pin,GPIO.LOW) def turn_light(colour, state): if state == 'off' and colour == 'all': led_all = [21,26] for x in led_all: lights_off(x) elif state == 'off' and colour == 'blue': lights_off(21) elif state == 'off' and colour == 'red': lights_off(26) elif state == 'on' and colour == 'all': led_all = [21,26] for x in led_all: lights_on(x) elif state == 'on' and colour == 'blue': lights_on(21) elif state == 'on' and colour == 'red': lights_on(26)
I use AutoTools SSH action to run the function and pass to arguments as strings.