Page 1 of 1

Help with converting old Wind AppleScript to Python

PostPosted: Sun Dec 04, 2022 7:33 am
by McJohn
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"

Re: Help with converting old Wind AppleScript to Python

PostPosted: Sun Dec 04, 2022 12:10 pm
by DaveL17
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.

Re: Help with converting old Wind AppleScript to Python

PostPosted: Sun Dec 04, 2022 2:24 pm
by McJohn
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:"

Re: Help with converting old Wind AppleScript to Python

PostPosted: Sun Dec 04, 2022 2:27 pm
by DaveL17
Whoops. Change 'else' to 'elif'.

'else' doesn't take conditionals.

Re: Help with converting old Wind AppleScript to Python

PostPosted: Sun Dec 04, 2022 2:59 pm
by McJohn
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

Re: Help with converting old Wind AppleScript to Python

PostPosted: Sun Dec 04, 2022 3:06 pm
by DaveL17
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

Re: Help with converting old Wind AppleScript to Python

PostPosted: Sun Dec 04, 2022 3:15 pm
by McJohn
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'

Re: Help with converting old Wind AppleScript to Python

PostPosted: Sun Dec 04, 2022 3:20 pm
by DaveL17
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"

Re: Help with converting old Wind AppleScript to Python

PostPosted: Sun Dec 04, 2022 3:29 pm
by McJohn
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

Re: Help with converting old Wind AppleScript to Python

PostPosted: Sun Dec 04, 2022 3:49 pm
by DaveL17
Glad to help.