[SOLVED]Possible to truncate variable and convert to number?

Posted on
Thu Mar 06, 2014 2:47 pm
eme jota ce offline
Posts: 618
Joined: Jul 09, 2009
Location: SW Florida

[SOLVED]Possible to truncate variable and convert to number?

The Autelis Plugin provides values for pool temperature and water temperature in the format of "81 F" or "86 F", not as the numbers 81 or 86.

I'd like to get these values into the new Indigoplot plugin. Indigoplot does not show water temp or air temp as properties to the Autelis device (probably because it's not a number, on/off state or anything that might be recognizable for graphing.) I tried creating an Indigo variable from the Autelis water / air temps, but the variables also have the "81 F" and "86 F" format.

Is there an Indigo feature that I'm not finding that will allow converting "81 F" variable to a variable that is the number 81? If not, can anyone provide a script that would do this?

Thanks,
MJC

Posted on
Thu Mar 06, 2014 3:39 pm
mat offline
Posts: 769
Joined: Nov 25, 2010
Location: Cambridgeshire - UK

Re: Possible to truncate variable and convert to number?

Something like this applescript should work.


replace the "variable containing 81 F" with the variable with the temperature in.
replace "your variable", with the variable name that you want to contain the number.


Code: Select all

set temp to (value of variable "variable containing 81 F")

set value of variable "your variable" to word 1 of temp as number



Use a trigger to execute the script when the first variable changes.

There may be an easier way, my scripting isn't great but it should work

Mat

GGGRRR EDIT because my dyslexia is so bad tonight

Late 2018 mini 10.14

Posted on
Thu Mar 06, 2014 4:02 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Possible to truncate variable and convert to number?

Or Python:

Code: Select all
# get the value from the plugin device
value = indigo.devices[DEVICEIDHERE].states["stateNameHere"]
# value now contains "81 F"

# split the value into two parts (using the space as the separator), and return the first part [0]
numberPart = s.split()[0]
# s.split() results in a list containing ["81", "F"], and the [0] subscript gets the first one
# so numberPart now contains "81"

# update the Indigo variable with the value
indigo.variable.updateValue(VARIDHERE, value=numberPart)


Note: Indigo variable values are always strings so you don't need to convert it to a number before updating the Indigo variable with the value.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Mar 07, 2014 9:06 am
eme jota ce offline
Posts: 618
Joined: Jul 09, 2009
Location: SW Florida

Re: Possible to truncate variable and convert to number?

mat wrote:
Something like this applescript should work.


replace the "variable containing 81 F" with the variable with the temperature in.
replace "your variable", with the variable name that you want to contain the number.


Code: Select all

set temp to (value of variable "variable containing 81 F")

set value of variable "your variable" to word 1 of temp as number



Use a trigger to execute the script when the first variable changes.

There may be an easier way, my scripting isn't great but it should work

Mat


Thanks Mat,

That code worked great.

Posted on
Fri Mar 07, 2014 9:11 am
eme jota ce offline
Posts: 618
Joined: Jul 09, 2009
Location: SW Florida

Re: Possible to truncate variable and convert to number?

jay (support) wrote:
Or Python:

Code: Select all
# get the value from the plugin device
value = indigo.devices[DEVICEIDHERE].states["stateNameHere"]
# value now contains "81 F"

# split the value into two parts (using the space as the separator), and return the first part [0]
numberPart = s.split()[0]
# s.split() results in a list containing ["81", "F"], and the [0] subscript gets the first one
# so numberPart now contains "81"

# update the Indigo variable with the value
indigo.variable.updateValue(VARIDHERE, value=numberPart)


Note: Indigo variable values are always strings so you don't need to convert it to a number before updating the Indigo variable with the value.


Thanks Jay,

Couldn't get it to work using this script. Double checked that I had copied the right VARID and DEVICEID into Python script, so my error is probably related to the value I used in "stateNameHere" When creating a Control Page in Indigo, that value is displayed by selecting "Temp Pool" from the drop down list in the Control Page editor. I tried inputting "Temp Pool" and "TempPool" into "stateNameHere" but neither worked.

If you have any suggestions or corrections, please do not hesitate to post. Greatly appreciate the comments you provided to help me understand the Python and also appreciate that your approach uses the device and variable ID's instead of the names.

MJC

Posted on
Fri Mar 07, 2014 10:54 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Possible to truncate variable and convert to number?

Select the device in the device list and then look below in the control area for the "Custom States" list (you may need to scroll the area down) and you'll find the actual state name there.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Mar 07, 2014 3:43 pm
eme jota ce offline
Posts: 618
Joined: Jul 09, 2009
Location: SW Florida

Re: [SOLVED]Possible to truncate variable and convert to num

Thanks Again Jay,

The error in the Python script window in Indigo says "embedded script: global name 's' is not defined"

Pasted code below from the Action window. Double checked the ID numbers and state name.

Code: Select all
# get the value from the plugin device
value = indigo.devices[1053601020].states["POOLTMP"]
# value now contains "81 F"

# split the value into two parts (using the space as the separator), and return the first part [0]
numberPart = s.split()[0]
# s.split() results in a list containing ["81", "F"], and the [0] subscript gets the first one
# so numberPart now contains "81"

# update the Indigo variable with the value
indigo.variable.updateValue(1992795691, value=numberPart)

Posted on
Fri Mar 07, 2014 4:08 pm
mat offline
Posts: 769
Joined: Nov 25, 2010
Location: Cambridgeshire - UK

Re: [SOLVED]Possible to truncate variable and convert to num

No python experience but try changing

Code: Select all
numberPart = s.split()[0]


to

Code: Select all
numberPart = value.split()[0]


Haven't got a clue but that would make some logical sense :evil:

Late 2018 mini 10.14

Posted on
Fri Mar 07, 2014 7:16 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: [SOLVED]Possible to truncate variable and convert to num

Yep - cut/paste error on my part - "s.split" should actually be "value.split"

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests