autovoice-dynamic-variable

AutoVoice has a few different built in variables, including an array that contains all the individual words in the recognized command, i.e. %avword1, %avword2 etc. That’s great if you know the position of the word you need the variable for, but if that position changes from one command to the next, it’s a bit trickier, but not much.

There are a couple of different situations where you might want to do this. The first is if you want to get the variable that contains a specific word that changes position from command to command. Let’s say you want to know which %avword contains the word “google” in a situation with possible commands along the lines of “ok google, find this” or “I need you to google this for me”. To do this you can use one of the methods for accessing data in an array, documented here. Specifically, you would use the  following action:

Variable Set %temp to %avword(#?google)

After that, you can use the following code to refer to the variable:

%avword(%temp)

Which will then contain “google”. %avword(#?google) essentially tells Tasker to search the array %avword for any variables that match “google”, and return their array number. So if the fifth word is “google”, %avword(#?google) will be 5. Then, by slapping a variable that contains that behind %avword, you’re actually asking for the array variable %avword5.

Note that %avword(%avword(#?google)) doesn’t work, but if anyone knows a way to make Tasker accept variable names that complex, let me know. Also note that the word after #? follows normal matching rules in Tasker, so use * as wildcards to make it match parts of words.

The other way you can use this is to get the variable for a dynamic word that you know the position of relative to another word. This is what I’ve actually used this for myself, as it allows you to get a variable that contains a dynamic word based on its position relative to a static word. For instance, I’ve used this to get the variables of words that immediately precede the words “seconds” and “minutes”. That way, if I use a command that include values in seconds and minutes, I can access the values of those regardless of how the command is phrased, as long as those denominations are used.

The way to do this is very similar to the above, you simply add some math to the whole thing. For instance, if I want to select the word in front of “second” or “seconds” in the command, I would use this action:

Variable Set %temp to %avword(#?second*)-1, Use Maths. 

You can then use %avword(%temp) to refer to the variable right in front of “second” or “seconds”.  Like above we found the array number of a variable containing a certain word, and simply subtracted 1 from it to get the one before it. By subtracting or adding other numbers you can select other words relative to the static one.

By using this method, I was able to get mentions of seconds and minutes as variables that I could then convert and feed into a timer app to start a timer. Another use I’ve seen for this is someone who wanted to get a variable containing the word immediately following “meet” in a command. By doing that, you can tell your voice assistant “hello there, meet John”, and then have a Say response that actually incorporates the name that followed “meet”.   I’ve also used this to integrate my Tasker todo list app with AutoVoice, allowing me to add items using voice. In that case I used this trick to find the word right before “list”, giving me a variable with the name of the list I wanted to add the item to.