If you have old TVs that cannot be controlled by network (exposed API or else), you might find this useful. I have a few TVs like that, and the only way I have to control them is with an IR emitter such as iTach FLEX (one of many options on the market) or the TV remote itself.
By using the FLEX, you can create network resources in ISY that send messages to the FLEX with the desired IR code representing a single digit/button on your remote. Nothing new here, I've had this for years. But since the A/V device types showed up on the ISY Portal for Alexa, I saw how you could now instantiate a virtual device and tie actions such as volume or channel changing to either programs, variables or network resources. This by itself is cool, but still posed a challenge: how to convert a spoken number (normally 2-3 digits) in the instruction "Alexa, set channel to XX on device YY" to single digits for processing by individual network resource calls?
To achieve this, I've created a series of programs/network resources that handle such a thing, with the use of some simple math to split the number into its forming single digits. Example: "Alexa, set channel to 29 on device Bedroom TV" has to be converted to 2 different calls to network resources that represent the "2" and then the "9", and they must be executed in order to obtain the expected result. Let's see the code:
Step 1. Create a State Variable (for the full channel number) and a Integer Variable (for each digit to process)
Both should be precision 0 so this trick works
Step 2. Create one network resource per digit (0 to 9)
This step is very dependent on what you are using to send the IR signal to the TV. Using iTach FLEX a network resource looks like this:
Exported version of the same network resource above:
POST /api/v1/irports/1/sendir HTTP/1.1
Host: xxx.xxx.x.xxx:80
User-Agent: Mozilla/4.0
Connection: Close
Content-Type: application/x-www-form-urlencoded
Content-Length: 294
{
"frequency": 38000,
"irCode": "340,86,20,3664",
"preamble": "340,169,20,20,20,20,20,64,20,20,20,20,20,20,20,20,20,20,20,64,20,64,20,20,20,64,20,64,20,64,20,64,20,64,20,20,20,20,20,20,20,20,20,64,20,20,20,20,20,20,20,64,20,64,20,64,20,64,20,20,20,64,20,64,20,64,20,1545",
"repeat": 1
}
Step 3. Create a program for each digit (0 to 9) to call each corresponding network resource from above
Step 4. Create a program to parse each digit you extract
Call all the single digit programs in order by the IF statement
Step 5. Create a program to trigger when the channel State Variable changes
The key here is to get each digit by applying some simple math
Full Channel DIV/ 10 = Digit 1. Example: 29 / 10 = 2.9 (saved in a prec 0 variable becomes 2)
Full Channel MOD% 10 = Digit 2. Example: 29 % 10 = 9
Step 6. Configure your A/V device on the ISY Portal
Step 7. You may need to forget the device and rediscover it in the Alexa app so it recognizes the changes
By now you should be able to ask "Alexa, set channel to 42 on deviceX" and voila! Watch it magically work!
DISCLAIMER: This will work as it is, with 2 digit channels. If you have digital TV, you might have 3 digit channels (ex 713). For that you would need to modify the logic of step 5 to parse the number 3 times with some more math:
Given channel 713, if you
divide it by 1 and mod 10, you get 3
divide it by 10 and mod 10, you get 1
divide it by 100 and mod 10, you get 7