Incrementing a variable and parsing string

Posted on
Sun Mar 22, 2020 11:23 am
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Incrementing a variable and parsing string

Jay,

Thanks very much for your help with this parser. It's taken me some time... but now that I'm on COVID-19 lockdown I'm giving the Python another go. I've set the variables in and tried running it.

Code: Select all
# Get the entire response string
vaux_response_string = indigo.variables[1925362107].value #VauxResponse variable
# I'm assuming that the list will always split into 5 items. This avoids setting
# each variable individually. If not, it can be done much like you do it in
# AppleScript by splitting into a list then just grabbing each item.
first_value, second_value, vaux_zone_number, vaux_bass_setting, vaux_treble_setting = vaux_response_string.split(",")
if "!s" in first_value.lower():
    # Get the boolean value of the logging variable and log if it's true
    if indigo.variables[767117833].getValue(bool): #vauxLogging_Enabled
        indigo.server.log(vaux_response_string)
   
if second_value == "40":
    # Log the zone and it's settings
    indigo.server.log("{}  {}  {}".format(vaux_zone_number, vaux_bass_setting, vaux_treble_setting))
   
    # This is a more Pythonic way of doing your zone number map
    zone_map = {
        "0": "Off",
        "1": "Kitchen",
        "2": "FamRm",
        "3": "Study",
        "4": "LivDnRm",
        "5": "GameRm",
        "6": "Patio",
        "7": "Pool",
        "8": "MBR",
    }
    # Save off the current zone name for use later in case it doesn't change for some reason
    vaux_zone_variable = indigo.variables[519871620] #VauxZoneName
    temp_zone_name = vaux_zone_variable.value
    # If the zone number from the list is in the map's keys, save off the new name and set the Indigo variable
    if vaux_zone_number in zone_map.keys():
        temp_zone_name = zone_map[vaux_zone_number]
        indigo.variable.updateValue(vaux_zone_variable, value=temp_zone_name)
       
    # Create the string that's going to be set in the variable and logged
    new_string_value = "{}  Bass {}  Treble {}".format(temp_zone_name, vaux_bass_setting, vaux_treble_setting)
    # Set the Indigo variable
    indigo.variable.updateValue(1963098885, value=new_string_value)
    # Log the string
    indigo.server.log(new_string_value)


Currently returning this error:

Mar 22, 2020 at 1:12:56 PM
Simple Serial Plugin Response from serial device (text): !S,1,5,1,2,27,27,6,8

, of length 22 characters
Script Error Vaux Parser Python.py: too many values to unpack
Script Error Exception Traceback (most recent call shown last):

Vaux Parser Python.py, line 6, at top level
ValueError: too many values to unpack



Error seems to be on this line:
first_value, second_value, vaux_zone_number, vaux_bass_setting, vaux_treble_setting = vaux_response_string.split(",")


Would appreciate your thoughts.

Posted on
Sun Mar 22, 2020 11:32 am
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Incrementing a variable and parsing string

Can you share the value of the variable that's accessed with:

Code: Select all
vaux_response_string = indigo.variables[1925362107].value

Take for example, this code which runs without error:
Code: Select all
x = "1,2,3,4,5,6"
a, b, c, d, e, f = x.split(',')

But this code results in the error you're seeing:
Code: Select all
x = "1,2,3,4,5,6,"
a, b, c, d, e, f = x.split(',')

Note the extra comma on the end of x.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Mar 22, 2020 12:14 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Incrementing a variable and parsing string

Hi Dave,

Thanks for the look. Actually, this is the bass/treble parser, and I had hit the volume control, resulting in the error. When changing bass/treble it works fine. Yaay!

I am going to try to adapt Jay's script to the rest of the parser, and will report back.

Stay healthy!

Ham

Posted on
Sun Mar 22, 2020 12:28 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Incrementing a variable and parsing string

Glad to hear that it's working for you.

Stay safe!

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Mar 22, 2020 8:15 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Incrementing a variable and parsing string

OK, I added a "parser selector" at the top to pick out the identifying number for whether the second variable represents an audio source/volume selection (1 or 2) or a bass/treble selection (4). In this way I was hoping to have an integrated script fire one or the other as listed below. However, while the second part (zone/volume, 1 or 2) seems to work, the first part will not fire. It does not throw an error. I tried inserting a log step to see if it was running at all but no. Any thoughts?

Code: Select all
# Get the entire response string
vaux_response_string = indigo.variables[1925362107].value #VauxResponse variable
# I'm assuming that the list will always split into 5 items. This avoids setting
# each variable individually. If not, it can be done much like you do it in
# AppleScript by splitting into a list then just grabbing each item.

indigo.variable.updateValue(1524463060, str(vaux_response_string[3:4]))  #update parser selector
#first_value, second_value, vaux_zone_number, fourth_value, vaux_source_name = vaux_response_string.split(",") #vaux_volume_number1, vaux_volume_number2, vaux_bass_setting, vaux_treble_setting = vaux_response_string.split(",")

#if "!s" in first_value.lower():
    # Get the boolean value of the logging variable and log if it's true
#if indigo.variables[767117833].getValue(bool): #vauxLogging_Enabled
    #indigo.server.log(vaux_response_string)
   
#if second_value == "40": Parser_selector below picks first number of value, 4, and should execute this script if true.
if indigo.variables[1524463060] == "4":
   vaux_response_string = indigo.variables[1925362107].value #VauxResponse variable
   first_value, second_value, vaux_zone_number, vaux_bass_setting, vaux_treble_setting = vaux_response_string.split(",")

    # Log the zone and it's settings
       indigo.server.log("{}  {}  {}".format(vaux_zone_number, vaux_bass_setting, vaux_treble_setting))
   
    # This is a more Pythonic way of doing your zone number map
       zone_map = {
           "0": "Off",
           "1": "Kitchen",
           "2": "FamRm",
           "3": "Study",
           "4": "LivDnRm",
           "5": "GameRm",
           "6": "Patio",
           "7": "Pool",
           "8": "MBR",
       }
    # Save off the current zone name for use later in case it doesn't change for some reason
       vaux_zone_variable = indigo.variables[519871620] #VauxZoneName
       temp_zone_name = vaux_zone_variable.value
    # If the zone number from the list is in the map's keys, save off the new name and set the Indigo variable
       if vaux_zone_number in zone_map.keys():
           temp_zone_name = zone_map[vaux_zone_number]
           indigo.variable.updateValue(vaux_zone_variable, value=temp_zone_name)
       
    # Create the string that's going to be set in the variable and logged
       new_string_value = "{}  Bass {}  Treble {}".format(vaux_zone_variable, vaux_bass_setting, vaux_treble_setting)
    # Set the Indigo variable
       indigo.variable.updateValue(1963098885, value=new_string_value)
    # Log the string
       indigo.server.log(new_string_value)
   
   
#if second_value == "1":
if indigo.variables[1524463060].value ==  ("1" or "2"):
   vaux_response_string = indigo.variables[1925362107].value #VauxResponse variable
   first_value, second_value, vaux_zone_number, fourth_value, vaux_source_setting, vaux_volume_number1, vaux_volume_number2, vaux_bass_setting, vaux_treble_setting = vaux_response_string.split(",")
    # Log the zone and its' settings
       indigo.server.log("{}  {}  {}".format(vaux_zone_number, vaux_source_setting, vaux_volume_number1))
   
    # This is a more Pythonic way of doing your zone number map
       zone_map = {
           "0": "Off",
           "1": "Kitchen",
             "2": "FamRm",
           "3": "Study",
           "4": "LivDnRm",
           "5": "GameRm",
           "6": "Patio",
           "7": "Pool",
           "8": "MBR",
       }
   
       source_map = {
           "0": "Off",
           "1": "Tuner",
           "2": "iTunes",
           "3": "AirPlay",
           "4": "Cable",
       }
   
   
   
    # Save off the current zone name for use later in case it doesn't change for some reason
       vaux_zone_variable = indigo.variables[519871620] #VauxZoneName
       temp_zone_name = vaux_zone_variable.value
    # If the zone number from the list is in the map's keys, save off the new name and set the Indigo variable
       if vaux_zone_number in zone_map.keys():
           temp_zone_name = zone_map[vaux_zone_number]
           indigo.variable.updateValue(vaux_zone_variable, value=temp_zone_name)
   
       vaux_source_variable = indigo.variables[1890620001] #VauxSourceName
       temp_source_name = vaux_source_variable.value 
       if vaux_source_setting in source_map.keys():
           temp_source_name = source_map[vaux_source_setting]
           indigo.variable.updateValue(vaux_source_variable, value=temp_source_name)   
       
       
        vaux_volume_variable = vaux_volume_number1   #vauxZoneVolume
        indigo.variable.updateValue(1699344874, vaux_volume_number1)
        vaux_source_setting = indigo.variables[1890620001]
       
    # Create the string that's going to be set in the variable and logged
       new_string_value = "{}  {}  {} ".format(temp_zone_name, temp_source_name, vaux_volume_number1)
    # Set the Indigo variable
       indigo.variable.updateValue(1932988370, value=new_string_value) #vauxResponseOutputShort
    # Log the string
       indigo.server.log(new_string_value)
   

Posted on
Mon Mar 23, 2020 5:00 am
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Incrementing a variable and parsing string

I'm having a bit of trouble following your script.

You've got some stuff in there multiple times, for example I think you do this at least three times:
Code: Select all
vaux_response_string = indigo.variables[1925362107].value #VauxResponse variable

Moreover, I think the reason that the first bit isn't working as you expect is because you may have messed up your index reference. The index of [3:4] gives you the fourth element (by your note, that would be 'fourth_value'), when I think you want 'faux_zone_number' which would be index [2] not index [3].

Consider this:
Code: Select all
a = [0,1,2,3,4,5]

print(a[3:4])

>>>[3]

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Mon Mar 23, 2020 7:13 am
FlyingDiver offline
User avatar
Posts: 7213
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Incrementing a variable and parsing string

DaveL17 wrote:
Moreover, I think the reason that the first bit isn't working as you expect is because you may have messed up your index reference. The index of [3:4] gives you the fourth element (by your note, that would be 'fourth_value'), when I think you want 'faux_zone_number' which would be index [2] not index [3].

Consider this:
Code: Select all
a = [0,1,2,3,4,5]

print(a[3:4])

>>>[3]


Actually '[3:4]' returns a list with one element, the fourth value in the original array. If you just want the value, and not a list containing that value, then you need to do just '[3]'.

To the OP -

Also, there's some logic errors in the code. For instance, you have:
Code: Select all
if indigo.variables[1524463060].value ==  ("1" or "2"):

I don't think this does what you think it does. It's doing a logical OR on two string variables (which always returns TRUE), then comparing that to a string value. Which will also always return TRUE (I think). What you probably wanted to write was:
Code: Select all
if indigo.variables[1524463060].value in ["1", "2"]:

Which only evaluates as TRUE if the variable's value is "1" or "2".

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Mar 23, 2020 7:09 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Incrementing a variable and parsing string

Thanks very much. Yes, it would be much clearer if you had the strings!

The bass/treble string is: !S,40,2,5,4
where !S is always there to identify the string
40 = specifies bass/treble string. <--- this is what is picked out with the parser selector, "if indigo.variables[1524463060].value in ["1", "2"]:"
2 = zone
5 = bass level
4= treble level

The zone/source/volume string is : !S,1,3,1,3,27,27,2,8
!S = string identifier
1 = specifies zone/source/volume string <--- this is what is picked out with the parser selector, "if indigo.variables[1524463060].value in ["1", "2"]:"
3 = zone
1 = who knows
3 = Source
27 = volume
27 = volume (if stereo pair)
2 = bass for that zone
8 = treble setting for that zone

Note that the selector picks the first value, "4" or "1", which I figured was easiest. So the idea is to use the parser selector to pick the correct type of string and allow each section of the script to run, if true. I might just be able to split the trigger in to two separate ones.

Much appreciated!

Posted on
Wed Mar 25, 2020 4:38 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Incrementing a variable and parsing string

Mis-labeled the second string above. Pls see that position 5 = source.

Also I was not clear that the reason the parsing blocks need to be separated is that when a string is returned it is of different lengths depending on if it is bass/treble control or source/volume control. As seen above, bass/treble is a 5 value string whereas source/volume is 9 values.

That's why this string for source/volume:

Response from serial device (text): !S,1,8,1,1,26,26,4,4

throws this error in the bass/treble section of the script:

Script Error embedded script: too many values to unpack
Script Error Exception Traceback (most recent call shown last):

embedded script, line 15, at top level
ValueError: too many values to unpack

Hence my trying to separate the blocks. I had it all set in AppleScript, and appreciate the help so far. I do have it working as two separate triggers with the blocks, both firing when the string changes. I tried to put the parser_selector lines into a conditional but no joy. So while it works and looks good on the phone, it throws an error from one of the triggers o the other every time.

Posted on
Wed Mar 25, 2020 6:45 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Incrementing a variable and parsing string

It doesn't look like the error you posted matches up with the script above. Can you please post your latest version and the error message you get? It will help nail down the problem.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Thu Mar 26, 2020 8:31 am
jay (support) offline
Site Admin
User avatar
Posts: 18216
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Incrementing a variable and parsing string

Here is a general pattern you can use to process any messages that have the rough form you specified above:

Code: Select all
# This is the format of the treble/bass string: "!S,40,2,5,4"
# This is the format of the zone/source/volume string: "!S,1,3,1,3,27,27,2,8"
vaux_response_string = indigo.variables[1925362107].value #VauxResponse variable

# This will split off the first two items - the '!S' which
# is not used (junk) and the second element (command) which is what
# you'll use to decipher the rest of the string (details)
junk, command, details = vaux_response_string.split(',', 2)
# details now has the string after the second comma in it, for instance "2,5,4"
# Determine what to do next
if command == "40":
   # It's the treble/bass string, so we know that there will be
   # 3 items in the details
   zone, bass, treble = details
   # Do whatever you need to do with those values here
elif command == "1":
    # It's the zone/source/volume string, so we know there will
    # be 7 items in the details
    zone, unknown, source, volume, volume2, bass, treble = details.split(',')
    # Do whatever you need to do with those values here


The written description: take the first two elements off the string because we know that the first item is exactly the same and can be safely ignored. The second element will determine how we process the rest of the string. Once we know what the command format is, then we can split the rest of the string into it's component parts.

This relies on the messages being formatted EXACTLY as you have specified them. If that's always the case, then this framework should work correctly. You can add more by adding extra elif clauses for each additional command message that you receive and care to parse. If you get an error that mentions value unpacking, it means that the message wasn't constructed as you specified. So either it had fewer than 3 elements when doing the first split, or the details didn't have the correct number of elements that you're examples specified.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Mar 28, 2020 9:11 am
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Incrementing a variable and parsing string

Jay, Dave and Joe, thank you all for your interest in helping me through this. So far, the two scripts work correctly independently of one another when put in separate triggers. However since both triggers fire with the string variable change, while one will work correctly for the zone/volume and the other will work correctly with bass/treble, the other trigger will log an error.

I combined the two adapted scripts in the hopes that the selector text that Jay put in would allow the script to run the appropriate code:

Code: Select all
vaux_response_string = indigo.variables[1925362107].value #VauxResponse variable
junk, command, details = vaux_response_string.split(',', 2)
# details now has the string after the second comma in it, for instance "2,5,4"
# Determine what to do next
if command == "1":
    # Log the zone and it's settings
   vaux_zone_number, fourth_value, vaux_source_setting, vaux_volume_number1, vaux_volume_number2, vaux_bass_setting, vaux_treble_setting = details.split(",")

   indigo.server.log("{}  {}  {}".format(vaux_zone_number, vaux_source_setting, vaux_volume_number1))
 

   
    # This is a more Pythonic way of doing your zone number map
zone_map = {
    "0": "Off",
    "1": "Kitchen",
    "2": "FamRm",
    "3": "Study",
    "4": "LivDnRm",
    "5": "GameRm",
    "6": "Patio",
    "7": "Pool",
    "8": "MBR",
}
   
source_map = {
    "0": "Off",
    "1": "Tuner",
    "2": "iTunes",
    "3": "AirPlay",
    "4": "Cable",
}
   
   
   
    # Save off the current zone name for use later in case it doesn't change for some reason
vaux_zone_variable = indigo.variables[519871620] #VauxZoneName
temp_zone_name = vaux_zone_variable.value
    # If the zone number from the list is in the map's keys, save off the new name and set the Indigo variable
if vaux_zone_number in zone_map.keys():
    temp_zone_name = zone_map[vaux_zone_number]
    indigo.variable.updateValue(vaux_zone_variable, value=temp_zone_name)
   
vaux_source_variable = indigo.variables[1890620001] #VauxSourceName
temp_source_name = vaux_source_variable.value 
if vaux_source_setting in source_map.keys():
        temp_source_name = source_map[vaux_source_setting]
        indigo.variable.updateValue(vaux_source_variable, value=temp_source_name)   
       
       
vaux_volume_variable = vaux_volume_number1   #vauxZoneVolume
indigo.variable.updateValue(1699344874, vaux_volume_number1)
vaux_source_setting = indigo.variables[1890620001]
       
    # Create the string that's going to be set in the variable and logged
new_string_value = "{}  {}  {} ".format(temp_zone_name, temp_source_name, vaux_volume_number1)
    # Set the Indigo variable
indigo.variable.updateValue(1932988370, value=new_string_value) #vauxResponseOutputShort
    # Log the string
indigo.server.log(new_string_value)




vaux_response_string = indigo.variables[1925362107].value #VauxResponse variable
junk, command, details = vaux_response_string.split(',', 2)
# details now has the string after the second comma in it, for instance "2,5,4"
# Determine what to do next
if command == "40":
    # Log the zone and it's settings
   #indigo.server.log("{}  {}  {}".format(vaux_zone_number, vaux_bass_setting, vaux_treble_setting))
   vaux_zone_number,vaux_bass_setting, vaux_treble_setting = details.split(",")
   indigo.server.log("{}  {}  {}".format(vaux_zone_number, vaux_bass_setting, vaux_treble_setting))
 
    # This is a more Pythonic way of doing your zone number map
   zone_map = {
    "0": "Off",
    "1": "Kitchen",
    "2": "FamRm",
    "3": "Study",
    "4": "LivDnRm",
    "5": "GameRm",
    "6": "Patio",
    "7": "Pool",
    "8": "MBR",
 }
    # Save off the current zone name for use later in case it doesn't change for some reason
vaux_zone_variable = indigo.variables[519871620] #VauxZoneName
temp_zone_name = vaux_zone_variable.value
# If the zone number from the list is in the map's keys, save off the new name and set the Indigo variable
if vaux_zone_number in zone_map.keys():
   temp_zone_name = zone_map[vaux_zone_number]
   indigo.variable.updateValue(vaux_zone_variable, value=temp_zone_name)
       
# Create the string that's going to be set in the variable and logged
new_string_value = "{}  Bass {}  Treble {}".format(temp_zone_name, vaux_bass_setting, vaux_treble_setting)
# Set the Indigo variable
indigo.variable.updateValue(1963098885, value=new_string_value)
# Log the string
indigo.server.log(new_string_value)


Here are the responses when setting source in the Game Room zone. The script runs correctly and both the zone/source/volume and bass/treble scripts run.

However, when trying to change the bass/treble, the script hangs on the zone/source/volume sections. So it looks like it is not passing over the zone/source/volume to execute only the bass/treble.

Code: Select all
Mar 28, 2020 at 10:56:59 AM

>>>>>>>>>>>>>>>>Turn off Game Room
   Simple Serial Plugin            Response from serial device (text): !S,1,5,0,0,0,0,6,8

, of length 20 characters
   Script                          5  0  0
   Script                          GameRm  Off  0
   Script                          GameRm  Bass 6  Treble 8

   Simple Serial Plugin            Response from serial device (text): !S,1,5,0,0,0,0,6,8

, of length 20 characters
   Simple Serial Plugin            Response from serial device (text): , of length 0 characters

>>>>>>>>>>>>>>>>>>>> Change Bass/Treble
   Simple Serial Plugin            Response from serial device (text): !S,40,5,6,7. <<<<<<<<<< 40 = bass/treble string, 5 = zone Game Room, 6 = bass setting, 7 = treble setting

, of length 13 characters
   Script Error                    Vaux Python Parser 200328.py: local variable 'vaux_zone_number' referenced before assignment
   Script Error                    Exception Traceback (most recent call shown last):

     Vaux Python Parser 200328.py, line 40, at top level
UnboundLocalError: local variable 'vaux_zone_number' referenced before assignment

>>>>>>>>>>>>>>>>> Again

   Simple Serial Plugin            Response from serial device (text): !S,40,5,6,6  <<<<<<<<<<< new string with change showing treble lowered to 6

, of length 13 characters
   Script Error                    Vaux Python Parser 200328.py: local variable 'vaux_zone_number' referenced before assignment
   Script Error                    Exception Traceback (most recent call shown last):

     Vaux Python Parser 200328.py, line 40, at top level
UnboundLocalError: local variable 'vaux_zone_number' referenced before assignment

>>>>>>>>>>>>>>>>> Again

   Simple Serial Plugin            Response from serial device (text): !S,40,5,6,5

, of length 13 characters
   Script Error                    Vaux Python Parser 200328.py: local variable 'vaux_zone_number' referenced before assignment
   Script Error                    Exception Traceback (most recent call shown last):

     Vaux Python Parser 200328.py, line 40, at top level
UnboundLocalError: local variable 'vaux_zone_number' referenced before assignment

>>>>>>>>>>>>change the volume once again. Now the script works correctly to process the zone/source/volume as well as bass/treble.

Mar 28, 2020 at 10:58:01 AM
   Simple Serial Plugin            Response from serial device (text): !S,1,5,1,1,29,29,6,5

, of length 22 characters
   Script                          5  1  29.  <<<<<<<Zone Source Volume
   Script                          GameRm  Tuner  29 <<<<<<<<<<<<< Correct
   Script                          GameRm  Bass 6  Treble 5  <<<<<<<<  Correct


Overall the simplest thing to do is to ignore the problem, because I rarely change the bass/treble, but I'd like to get it to work if possible. Maybe the easiest thing is to just put these two separate scripts into different triggers with a conditional based the selector, but I could not figure out how to get to resolve to "true" to let the trigger execute.

Posted on
Sat Mar 28, 2020 9:32 am
FlyingDiver offline
User avatar
Posts: 7213
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Incrementing a variable and parsing string

Fix this:
Code: Select all
    # Save off the current zone name for use later in case it doesn't change for some reason
vaux_zone_variable = indigo.variables[519871620] #VauxZoneName
temp_zone_name = vaux_zone_variable.value

To this:
Code: Select all
    # Save off the current zone name for use later in case it doesn't change for some reason
vaux_zone_variable = indigo.variables[519871620] #VauxZoneName
vaux_zone_name = vaux_zone_variable.value

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sat Mar 28, 2020 10:32 am
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Incrementing a variable and parsing string

Thanks very much! However, after modifying the script, when changing the bass/treble it is still throwing the same error in the zone/source/volume section at line 40 up top, suggesting that it is not passing over that section despite the selector text being false.

Code: Select all
vaux_response_string = indigo.variables[1925362107].value #VauxResponse variable
junk, command, details = vaux_response_string.split(',', 2)
# details now has the string after the second comma in it, for instance "2,5,4"
# Determine what to do next
if command == "1":
    # Log the zone and it's settings
   vaux_zone_number, fourth_value, vaux_source_setting, vaux_volume_number1, vaux_volume_number2, vaux_bass_setting, vaux_treble_setting = details.split(",")

   indigo.server.log("{}  {}  {}".format(vaux_zone_number, vaux_source_setting, vaux_volume_number1))
 

   
    # This is a more Pythonic way of doing your zone number map
zone_map = {
    "0": "Off",
    "1": "Kitchen",
    "2": "FamRm",
    "3": "Study",
    "4": "LivDnRm",
    "5": "GameRm",
    "6": "Patio",
    "7": "Pool",
    "8": "MBR",
}
   
source_map = {
    "0": "Off",
    "1": "Tuner",
    "2": "iTunes",
    "3": "AirPlay",
    "4": "Cable",
}
   
   
   
    # Save off the current zone name for use later in case it doesn't change for some reason
vaux_zone_variable = indigo.variables[519871620] #VauxZoneName
vaux_zone_name = vaux_zone_variable.value
    # If the zone number from the list is in the map's keys, save off the new name and set the Indigo variable
if vaux_zone_number in zone_map.keys():
    vaux_zone_name = zone_map[vaux_zone_number]
    indigo.variable.updateValue(vaux_zone_variable, value=temp_zone_name)
   
vaux_source_variable = indigo.variables[1890620001] #VauxSourceName
temp_source_name = vaux_source_variable.value 
if vaux_source_setting in source_map.keys():
        temp_source_name = source_map[vaux_source_setting]
        indigo.variable.updateValue(vaux_source_variable, value=temp_source_name)   
       
       
vaux_volume_variable = vaux_volume_number1   #vauxZoneVolume
indigo.variable.updateValue(1699344874, vaux_volume_number1)
vaux_source_setting = indigo.variables[1890620001]
       
    # Create the string that's going to be set in the variable and logged
new_string_value = "{}  {}  {} ".format(temp_zone_name, temp_source_name, vaux_volume_number1)
    # Set the Indigo variable
indigo.variable.updateValue(1932988370, value=new_string_value) #vauxResponseOutputShort
    # Log the string
indigo.server.log(new_string_value)




vaux_response_string = indigo.variables[1925362107].value #VauxResponse variable
junk, command, details = vaux_response_string.split(',', 2)
# details now has the string after the second comma in it, for instance "2,5,4"
# Determine what to do next
if command == "40":
    # Log the zone and it's settings
   #indigo.server.log("{}  {}  {}".format(vaux_zone_number, vaux_bass_setting, vaux_treble_setting))
   vaux_zone_number,vaux_bass_setting, vaux_treble_setting = details.split(",")
   indigo.server.log("{}  {}  {}".format(vaux_zone_number, vaux_bass_setting, vaux_treble_setting))
 
    # This is a more Pythonic way of doing your zone number map
   zone_map = {
    "0": "Off",
    "1": "Kitchen",
    "2": "FamRm",
    "3": "Study",
    "4": "LivDnRm",
    "5": "GameRm",
    "6": "Patio",
    "7": "Pool",
    "8": "MBR",
 }
    # Save off the current zone name for use later in case it doesn't change for some reason
vaux_zone_variable = indigo.variables[519871620] #VauxZoneName
temp_zone_name = vaux_zone_variable.value
# If the zone number from the list is in the map's keys, save off the new name and set the Indigo variable
if vaux_zone_number in zone_map.keys():
   temp_zone_name = zone_map[vaux_zone_number]
   indigo.variable.updateValue(vaux_zone_variable, value=temp_zone_name)
       
# Create the string that's going to be set in the variable and logged
new_string_value = "{}  Bass {}  Treble {}".format(temp_zone_name, vaux_bass_setting, vaux_treble_setting)
# Set the Indigo variable
indigo.variable.updateValue(1963098885, value=new_string_value)
# Log the string
indigo.server.log(new_string_value)


Shouldn't this:
Code: Select all
vaux_response_string = indigo.variables[1925362107].value #VauxResponse variable
junk, command, details = vaux_response_string.split(',', 2)
# details now has the string after the second comma in it, for instance "2,5,4"
# Determine what to do next
if command == "1":


cause it to skip to the next section if "if command ==1" is false? Does not appear to be working that way.

maybe a simpler thing would be to have two separate triggers. Is it possible to write that section as a trigger conditional, so if it resolves to true then the trigger executes? That way it would work with separate triggers. I tried inserting

Code: Select all
vaux_response_string = indigo.variables[1925362107].value #VauxResponse variable
junk, command, details = vaux_response_string.split(',', 2)
# details now has the string after the second comma in it, for instance "2,5,4"
# Determine what to do next
if command == "40":
   pass


into a separate trigger conditional, but it throws this error: "
Code: Select all
  Error                           failed to evaluate embedded script conditonal
   Error                           script did not return a valid boolean value

Posted on
Sat Mar 28, 2020 10:52 am
FlyingDiver offline
User avatar
Posts: 7213
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Incrementing a variable and parsing string

Your indentation was totally inconsistent. It's the indentation that marks the limits of the "if" block. I cleaned that up, plus a bunch of other simplification:

Code: Select all
# This is a more Pythonic way of doing your zone number map
zone_map = {
    "0": "Off",
    "1": "Kitchen",
    "2": "FamRm",
    "3": "Study",
    "4": "LivDnRm",
    "5": "GameRm",
    "6": "Patio",
    "7": "Pool",
    "8": "MBR",
}
   
source_map = {
    "0": "Off",
    "1": "Tuner",
    "2": "iTunes",
    "3": "AirPlay",
    "4": "Cable",
}
   

vaux_response_string = indigo.variables[1925362107].value #VauxResponse variable
junk, command, vaux_zone_number, details = vaux_response_string.split(',', 3)
# details now has the string after the third comma in it, for instance "5,4"

# Save off the current zone name for use later in case it doesn't change for some reason
vaux_zone_variable = indigo.variables[519871620] #VauxZoneName
vaux_zone_name = vaux_zone_variable.value
if vaux_zone_number in zone_map.keys():
    vaux_zone_name = zone_map[vaux_zone_number]
    indigo.variable.updateValue(vaux_zone_variable, value=temp_zone_name)


if command == "1":
    # Log the zone and it's settings
   fourth_value, vaux_source_setting, vaux_volume_number1, vaux_volume_number2, vaux_bass_setting, vaux_treble_setting = details.split(",")
   indigo.server.log("{}  {}  {}".format(vaux_source_setting, vaux_volume_number1))
   
    vaux_source_variable = indigo.variables[1890620001] #VauxSourceName
    temp_source_name = vaux_source_variable.value 
    if vaux_source_setting in source_map.keys():
        temp_source_name = source_map[vaux_source_setting]
        indigo.variable.updateValue(vaux_source_variable, value=temp_source_name)   
       
    vaux_volume_variable = vaux_volume_number1   #vauxZoneVolume
    indigo.variable.updateValue(1699344874, vaux_volume_number1)
    vaux_source_setting = indigo.variables[1890620001]
       
    indigo.variable.updateValue(1932988370, value="{}  {}  {} ".format(temp_zone_name, temp_source_name, vaux_volume_number1))
    indigo.server.log(new_string_value)

if command == "40":
    vaux_bass_setting, vaux_treble_setting = details.split(",")
    indigo.server.log("{}  {}  {}".format(vaux_zone_number, vaux_bass_setting, vaux_treble_setting))       
    indigo.variable.updateValue(1963098885, value="{}  Bass {}  Treble {}".format(temp_zone_name, vaux_bass_setting, vaux_treble_setting))
    indigo.server.log(new_string_value)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Who is online

Users browsing this forum: No registered users and 1 guest

cron