Help with converting old Wind AppleScript to Python

Posted on
Sun Dec 04, 2022 7:33 am
McJohn offline
User avatar
Posts: 631
Joined: Dec 18, 2012
Location: The Netherlands

Help with converting old Wind AppleScript to Python

Can someone help us with the puzzle below?

(It's an old AppleScript that converts wind in mps to the Beaufort scale)
Thanks in advance.

John


Code: Select all
   if value of variable "Wind" as number ≤0.2 then
      set value of variable “Wind Beaufort to "0"
   else if value of variable "Wind" as number >0.2 and value of variable "Wind" as number <3 then
      set value of variable "Wind Beaufort" to "1"
   else if value of variable "Wind" as number >2 and value of variable "Wind" as number <4 then
      set value of variable "Wind Beaufort" to "2"

Posted on
Sun Dec 04, 2022 12:10 pm
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help with converting old Wind AppleScript to Python

I think this is right (it's untested). Note that there is overlap between the second and third conditions.

Code: Select all
wind = float(indigo.variables["Wind"].value)
wind_b = indigo.variables["Wind Beaufort"]

if wind <= 0.2:
    indigo.variable.updateValue(wind_b, "0")
elif 0.2 < wind < 3:
    indigo.variable.updateValue(wind_b, "1")
elif 2 < wind < 4:
    indigo.variable.updateValue(wind_b, "2")


UPDATE: corrects error described below.
Last edited by DaveL17 on Sun Dec 04, 2022 2:29 pm, edited 1 time in total.

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

[My Plugins] - [My Forums]

Posted on
Sun Dec 04, 2022 2:24 pm
McJohn offline
User avatar
Posts: 631
Joined: Dec 18, 2012
Location: The Netherlands

Re: Help with converting old Wind AppleScript to Python

Thank you so much for the fast answer Dave!

Sorry, it says:

Code: Select all
Script Error                    embedded script error:
   Script Error                    expected ':'
   Script Error                    around line 8 - "else 3 < wind < 4:"

Posted on
Sun Dec 04, 2022 2:27 pm
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help with converting old Wind AppleScript to Python

Whoops. Change 'else' to 'elif'.

'else' doesn't take conditionals.

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

[My Plugins] - [My Forums]

Posted on
Sun Dec 04, 2022 2:59 pm
McJohn offline
User avatar
Posts: 631
Joined: Dec 18, 2012
Location: The Netherlands

Re: Help with converting old Wind AppleScript to Python

Thanks! It's working!

I was wondering what came after "else" for the rest of the Beaufort scale but now I get it!

And I learned something from you again. Happy customer!

John

Posted on
Sun Dec 04, 2022 3:06 pm
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help with converting old Wind AppleScript to Python

You're welcome. "else" will be run if all the other "if"/"elif" conditionals don't fire.

Psuedo code:
Code: Select all
if x == 1:
    # stuff
elif x == 2:
    # stuff
else:
    # x doesn't equal 1 or 2

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

[My Plugins] - [My Forums]

Posted on
Sun Dec 04, 2022 3:15 pm
McJohn offline
User avatar
Posts: 631
Joined: Dec 18, 2012
Location: The Netherlands

Re: Help with converting old Wind AppleScript to Python

Thanks for the Python course!

Sorry, want to receive the data directly from the Indigo device:

Code: Select all
wind = float(indigo.devices[1512643088].states['wind_average'].value)


(the data from this Indigo Device, Wind_Average = at the moment "1.9")

But then the script says:

Script Error 'float' object has no attribute 'value'
Script Error Exception Traceback (most recent call shown last):

embedded script, line 1, at top level
AttributeError: 'float' object has no attribute 'value'

Posted on
Sun Dec 04, 2022 3:20 pm
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help with converting old Wind AppleScript to Python

No problem whatsoever.

When getting values from devices, use this syntax:
Code: Select all
indigo.devices[1307330006].states["windSpeed"]

If the state value is a string, and you need to convert it:
Code: Select all
float(indigo.devices[1307330006].states["windSpeed"])

You can get the reference for any device state by right-clicking on it and selecting "Copy Python Reference"

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

[My Plugins] - [My Forums]

Posted on
Sun Dec 04, 2022 3:29 pm
McJohn offline
User avatar
Posts: 631
Joined: Dec 18, 2012
Location: The Netherlands

Re: Help with converting old Wind AppleScript to Python

Thanks for your patience.

This one did the trick:

Code: Select all
wind = indigo.devices[1512643088].states["wind_average"]


It's working now with the Weatherflow Tempest weather station.

Nice winter job :D

All the best,

John

Posted on
Sun Dec 04, 2022 3:49 pm
DaveL17 offline
User avatar
Posts: 6759
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help with converting old Wind AppleScript to Python

Glad to help.

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

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 10 guests