Replacing period with comma and multiply by factor

Posted on
Thu Oct 02, 2014 12:22 am
haavarda offline
User avatar
Posts: 702
Joined: Aug 18, 2012
Location: Norway

Replacing period with comma and multiply by factor

Hi,
I am trying to tweak a variable to adjust my power meter.
The measured power from the meter is displayed like this : 2000.4578. I have stored this number in a variable name "meter". And I have a variable named "meter_adjusted".
I would like to multiply "meter" with a factor of 1,6. And then save the value to "meter_adjusted".

I tried to do it with apple script but run in to a issue that I could not multiply a number that had period and not comma in the string.

Anyone that can share some light?

Håvard

Håvard

Posted on
Thu Oct 02, 2014 2:59 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Replacing period with comma and multiply by factor

Assuming the 2000.4578 is using a language/notation where the period is a decimal point - which makes that number somewhere between 2000 and 2001 (?) - then you should be able to multiply it by 1.6 (not 1,6).

I presume in your native language, decimal places are notated as a comma?

Python as it understand it uses what I would call English notation - though I think US also use more commas than UK so I'm not 100% on that.

Posted on
Thu Oct 02, 2014 3:28 am
haavarda offline
User avatar
Posts: 702
Joined: Aug 18, 2012
Location: Norway

Re: Replacing period with comma and multiply by factor

When I get the state containing wattage it is presented with "." as a decimal separator.

When I try to do this with apple script I do the following:

Code: Select all
set effekt to (value of variable "Forbruk" as number)
set value of variable "Forbruk_justert" to {effekt * 1.6} as string


But I get an error:
Cant make "1114.896" into type number.
If I manually change the variable to 1114,896 it works. So I was thinking that I could try to make a script that replaces the period with a comma?
And I assume that python is the way to go here...

Håvard

Håvard

Posted on
Thu Oct 02, 2014 3:51 am
colinpartridge offline
Posts: 373
Joined: Jan 13, 2014
Location: London, UK

Re: Replacing period with comma and multiply by factor

haavarda wrote:

And I assume that python is the way to go here...

Håvard


I think you know the answer to that....I'm still at the copy and paste level of "learning" Python but what I have done has been reliable. I also think it is good practice to use the ID of an item rather than its name, that way even if you change the name of a device/variable etc the script will still work. At least thats my understanding.

Cheers

Colin

Posted on
Thu Oct 02, 2014 3:55 am
haavarda offline
User avatar
Posts: 702
Joined: Aug 18, 2012
Location: Norway

Re: Replacing period with comma and multiply by factor

I also think it is good practice to use the ID of an item rather than its name


I agree. But unfortunately the example I based this on used specific names...

Håvard

Håvard

Posted on
Thu Oct 02, 2014 4:05 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Replacing period with comma and multiply by factor

If nobody replies before me, I'll check it tonight. I think in python it's called Integer not number, but I can't remember the syntax.

Posted on
Thu Oct 02, 2014 4:12 am
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Replacing period with comma and multiply by factor

Hi Håvard -

The brute force method with a Python string is this:
Code: Select all
x = "2000.4578"
y = x.replace('.',',')
This expression will replace all instances of DOT with COMMA. Since you are getting the value from a variable, it's already a string so that part's easy ( and possibly the reason you're getting that error.)

But you can't do math on strings, so you need to convert it to a float:
Code: Select all
effekt = "2000.4578"
effecktNew = effekt.replace('.',',')
effecktFloat = float(effecktNew)
Forbruk_justert = effecktFloat * 1,6

I haven't tested this because I use "." instead of ",". :D

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

[My Plugins] - [My Forums]

Posted on
Thu Oct 02, 2014 4:40 am
haavarda offline
User avatar
Posts: 702
Joined: Aug 18, 2012
Location: Norway

Re: Replacing period with comma and multiply by factor

Will this take 1 variable, replace period with comma and multiply by factor and insert that value in another variable?
Sorry but I am a beginner at this scripting language...

Håvard

Håvard

Posted on
Thu Oct 02, 2014 5:56 am
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Replacing period with comma and multiply by factor

Nearly.

Code: Select all
# Get the value of variable 1
theValue = indigo.variables[123].value

# Put the answer in variable 2
indigo.variable.updateValue(124, theNewValue)


You need to combine this with the code above. (Sorry for the short answer, but writing code on a phone is hard.) :D

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

[My Plugins] - [My Forums]

Posted on
Thu Oct 02, 2014 6:20 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Replacing period with comma and multiply by factor

Dave is correct with both parts, but I think the comma conversion is a red-herring.

I think (though might be wrong) that Python and AppleScript both need periods, not commas, in their numerical representations.

So you should just do 1) from Dave's second answer. Then the effeckfloat bits from his first answer, ignoring the replace() line, then do 2) from his second answer.

Posted on
Thu Oct 02, 2014 7:07 am
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Replacing period with comma and multiply by factor

howartp wrote:
Dave is correct with both parts, but I think the comma conversion is a red-herring.

I think (though might be wrong) that Python and AppleScript both need periods, not commas, in their numerical representations.

So you should just do 1) from Dave's second answer. Then the effeckfloat bits from his first answer, ignoring the replace() line, then do 2) from his second answer.

This is where I'm a bit out to sea.

I always assumed that Indigo would take its cue from the OS. That is, if the OS is set to 123,45 then that's what Indigo would use.

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

[My Plugins] - [My Forums]

Posted on
Thu Oct 02, 2014 10:12 am
DomoPat offline
User avatar
Posts: 210
Joined: Jul 17, 2010
Location: Toulouse, France

Re: Replacing period with comma and multiply by factor

Instead of:
set effekt to (value of variable "Forbruk" as number)

can you try:
set effekt to (value of variable "Forbruk" as real)

it works for me.

Patrick

Posted on
Thu Oct 02, 2014 10:53 am
jay (support) offline
Site Admin
User avatar
Posts: 18216
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Replacing period with comma and multiply by factor

DaveL17 wrote:
I always assumed that Indigo would take its cue from the OS. That is, if the OS is set to 123,45 then that's what Indigo would use.


Indigo isn't involved at all - Python or AppleScript is doing the type coercion. For variables, Indigo always treats them as strings (for simplicity) so whatever is using them has to do the coercion.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Oct 02, 2014 12:43 pm
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Replacing period with comma and multiply by factor

Code: Select all
effekt = indigo.variables[123456].value
Forbruk_justert = float(effekt) * 1.6
indigo.variable.updateValue(123789, str(Forbruk_justert))

That python code snippet works for me (in UK) in a script action.

123456 is the effect variable ID, 123789 is the Forbruk_justert variable ID.

Peter

Posted on
Thu Oct 02, 2014 1:50 pm
haavarda offline
User avatar
Posts: 702
Joined: Aug 18, 2012
Location: Norway

Re: Replacing period with comma and multiply by factor

Thanks for all the feedback.

Code: Select all
effekt = indigo.variables[123456].value
Forbruk_justert = float(effekt) * 1.6
indigo.variable.updateValue(123789, str(Forbruk_justert))


This script works perfectly.
Thanks for your help.


Håvard

Håvard

Who is online

Users browsing this forum: No registered users and 2 guests