Starting out with Python

Posted on
Tue Aug 21, 2018 8:57 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Starting out with Python

Thanks to everyone for helping me try to figure this out. I added ".getValue(bool)" to the end of the device and it returns 'true' or 'false' correctly as below and also correctly using Dave's if/else.

Code: Select all
Garage1 = indigo.devices[308749424].binaryInputs
Garage1_State = indigo.variables[552843942].getValue(bool)

GarageAutoClose = indigo.variables[1770515678].getValue(bool)

indigo.server.log(u"{0}".format(Garage1))
indigo.server.log(u"{0}".format(Garage1_State))
indigo.server.log(u"{0}".format(GarageAutoClose))

if Garage1_State:
    indigo.server.log('True')
else:
    indigo.server.log('False')

if GarageAutoClose :
    indigo.server.log('True')
else:
    indigo.server.log('False')



if Garage1_State and GarageAutoClose == 'true':
   indigo.iodevice.setBinaryOutput(308749424, 0, True)

Log:
Script                          [True]  <<<<<<<<<< state of the IO linc. True = open
Script                          True     <<<<<<<<<< IOLinc state stuck in a variable and accessed by the first script.
Script                          True     <<<<<<<<<< GarageAutoClose variable value from the first  part of the script
Script                          True     <<<<<<<<<< IOLinc state stuck in the variable "Garage1_State and accessed by the if/else script.
Script                          True     <<<<<<<<<< GarageAutoClose variable value accessed from the if/else script


but the IOLinc does not fire, even though the door is open and Autoclose is "true". Any thoughts? The IOLinc will fire if the script is wrong, as in the prior examples, so it looks like communication with the device is fine.

One other quick question: how would one write the conditional if, say, one wanted to execute an action if one value was false and the other true? or a greater than/less than, etc? Is there a compilation of conditional statements? I use those a lot and did see a thread, but no actual conditionals posted.

Posted on
Wed Aug 22, 2018 5:43 am
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Starting out with Python

As I mentioned above, the value you're pulling from the IO Linc is a Python list and not a boolean, so as long as there's a value within the list (there always should be) it will evaluate to True no matter what it contains. You need to access the element inside the list using its index.

Code: Select all
my_list = [1, 2]

print(my_list)
print(my_list[0])
print(my_list[1])

Output:
Code: Select all
[1, 2]
1
2

Second, you've now accessed the boolean value of GarageAutoClose, so change this line:
Code: Select all
if Garage1_State and GarageAutoClose == 'true':

To this:
Code: Select all
if Garage1_State and GarageAutoClose:

Here's an adjusted script:
Code: Select all
Garage1 = indigo.devices[308749424].binaryInputs[0]
Garage1_State = indigo.variables[552843942].getValue(bool)
GarageAutoClose = indigo.variables[1770515678].getValue(bool)

if Garage1_State and GarageAutoClose:
   indigo.iodevice.setBinaryOutput(308749424, 0, True)

But moreover, it seems that you are saying that you want to close the door when Garage1_State and GarageAutoClose are both True. Is that what you want?

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

[My Plugins] - [My Forums]

Posted on
Thu Aug 23, 2018 4:51 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Starting out with Python

I think the script works! Thanks very much for everyone's assistance. BTW, any thoughts on the other conditional comparisons?

Posted on
Thu Aug 23, 2018 5:23 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Starting out with Python

hamw wrote:
I think the script works! Thanks very much for everyone's assistance. BTW, any thoughts on the other conditional comparisons?


I'm afraid I've lost track of any other questions - perhaps restate the things that aren't currently addressed (or start a separate thread).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Aug 23, 2018 7:29 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Starting out with Python

Jay, here is a link to the other thread. I looked in the referenced tutorial but didn’t see anything about how to write conditionals. Would be great to have a few examples. Appreciate your help.

viewtopic.php?f=107&t=19933&hilit=Conditional

Posted on
Fri Aug 24, 2018 4:52 am
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Starting out with Python

The examples of conditional expressions are there, but the tutorial is about how to do scripting in Indigo not how to do scripting in Python. Start by looking at some tutorials online about equality. You already have an equality conditional in your script:
Code: Select all
if Garage1_State and GarageAutoClose:
   indigo.iodevice.setBinaryOutput(308749424, 0, True)

In pseudo-code, this is saying:
Code: Select all
if Garage1_State equals True and GarageAutoClose also equals True
    then set the binary output zero of the device with ID 308749424 to True

Here are some ways to do comparisons:
Code: Select all
x != 1         # does not equal
y == 'true'    # equals
z > 123        # greater than
a is b         # identities are the same
b is not c     # identities are not the same

There are lots of comparison operators and they can be broadly grouped into at least two types: value comparisons and identity comparisons. That is, do two items have the same value or are the two items the same thing. This is a bit too much to describe here--there's tons of guides out there that describes this stuff.

Secondly, there's the if/then/else type stuff. You have an example of this in your script, too. There is also lots of stuff online about Python if/then/else as well as 'while' and 'finally' :D.

There's absolutely nothing special about doing comparisons and conditionals in Indigo with respect to Python. The 'difference' is how we get the values in order to compare them, and you have examples of this in your script, too.
Code: Select all
Garage1 = indigo.devices[308749424].binaryInputs[0]
Garage1_State = indigo.variables[552843942].getValue(bool)

Probably the only truly tricky bit is to find all the states of a device. And this bit's not that tricky.
Code: Select all
dev = indigo.devices[12345678]            # get a copy (or instance) of the indigo device (see 'identity' above)
states = dev.states                       # inspect the device instance and grab the dict of its states
indigo.server.log(u"{0}".format(states))  # Print the dict of states to the Indigo log

Note how 'dev' in the first line shows up in the second line and how the variable 'states' in the second line shows up in the third line. Indigo quite handily also tells us what type of value the state is (which is nice). There can be lots of states--here's a Fantastic Weather Plugin device's states:
Code: Select all
states : States : (dict)
     alertCount : 1 (integer)
     alertDescription1 : ...FLASH FLOOD WATCH REMAINS IN EFFECT TONIGHT INTO TUESDAY... .Showers and thunderstorms will continue to deposit heavy rainfall over parts of southern Wisconsin tonight. Parts of west Madison has received between 2 and 5 inches of rainfall so far. A Flash Flood Warning remains in effect this evening for this area. Elsewhere, a Flash Flood Watch remains in effect for tonight. Occasional showers and thunderstorms will produce heavy rainfall tonight, possibly lasting into into Tuesday morning over southeast and east central Wisconsin. Several inches of additional rain may fall over southern Wisconsin during this time. Flash flooding may occur especially in urban areas or areas that received several inches of rain last week. ...FLASH FLOOD WATCH REMAINS IN EFFECT UNTIL 4 AM CDT TUESDAY... The Flash Flood Watch continues for * A portion of south central Wisconsin, including the following areas, Columbia, Dane, Green, Iowa, Lafayette, Rock, and Sauk. * Until 4 AM CDT Tuesday * Periods of moderate to heavy rainfall tonight. 2 to 4 inches of rainfall is expected with locally higher amounts. * Flash flooding in urban areas will be possible if 1 to 3 inches of rain or more falls in a short period of time. Flash flooding will be possible in rural areas that have already received several inches of rain last week. (string)
     alertDescription2 :   (string)
     alertDescription2.ui :   (string)
     alertDescription3 :   (string)
     alertDescription3.ui :   (string)
     alertDescription4 :   (string)
     alertDescription4.ui :   (string)
     alertDescription5 :   (string)
     alertDescription5.ui :   (string)
     alertExpires1 : 2018-08-21 04:00 (string)
     alertExpires2 :   (string)
     alertExpires2.ui :   (string)
     alertExpires3 :   (string)
     alertExpires3.ui :   (string)
     alertExpires4 :   (string)
     alertExpires4.ui :   (string)
     alertExpires5 :   (string)
     alertExpires5.ui :   (string)
     alertRegions1 : [u'Columbia', u'Dane', u'Green', u'Iowa', u'Lafayette', u'Rock', u'Sauk'] (string)
     alertRegions2 :   (string)
     alertRegions2.ui :   (string)
     alertRegions3 :   (string)
     alertRegions3.ui :   (string)
     alertRegions4 :   (string)
     alertRegions4.ui :   (string)
     alertRegions5 :   (string)
     alertRegions5.ui :   (string)
     alertSeverity1 : warning (string)
     alertSeverity2 :   (string)
     alertSeverity2.ui :   (string)
     alertSeverity3 :   (string)
     alertSeverity3.ui :   (string)
     alertSeverity4 :   (string)
     alertSeverity4.ui :   (string)
     alertSeverity5 :   (string)
     alertSeverity5.ui :   (string)
     alertStatus : true (bool)
     alertStatus.ui : True (string)
     alertTime1 : 2018-08-20 18:46 (string)
     alertTime2 :   (string)
     alertTime2.ui :   (string)
     alertTime3 :   (string)
     alertTime3.ui :   (string)
     alertTime4 :   (string)
     alertTime4.ui :   (string)
     alertTime5 :   (string)
     alertTime5.ui :   (string)
     alertTitle1 : Flash Flood Watch (string)
     alertTitle2 :   (string)
     alertTitle2.ui :   (string)
     alertTitle3 :   (string)
     alertTitle3.ui :   (string)
     alertTitle4 :   (string)
     alertTitle4.ui :   (string)
     alertTitle5 :   (string)
     alertTitle5.ui :   (string)
     alertUri1 : https://alerts.weather.gov/cap/wwacapget.php?x=WI125AB69F1168.FlashFloodWatch.125AB6AC1ED0WI.MKXFFAMKX.e993958cbc4700746e8b4380784edbb7 (string)
     alertUri2 :   (string)
     alertUri2.ui :   (string)
     alertUri3 :   (string)
     alertUri3.ui :   (string)
     alertUri4 :   (string)
     alertUri4.ui :   (string)
     alertUri5 :   (string)
     alertUri5.ui :   (string)
     apparentTemperature : 69.27 (real)
     apparentTemperature.ui : 69° (string)
     apparentTemperatureIcon : 69 (real)
     cloudCover : 100 (real)
     cloudCover.ui : 100% (string)
     cloudCoverIcon : 100 (real)
     currentObservation : Last updated on Aug 20, 19:16 PM -0500 (string)
     currentObservation24hr : 08-20-2018 07:16 PM (string)
     currentObservationEpoch : 1534810570 (integer)
     dewpoint : 65.5 (real)
     dewpoint.ui : 66° (string)
     dewpointIcon : 66 (real)
     humidity : 90 (real)
     humidity.ui : 90% (string)
     humidityIcon : 90 (real)
     icon : rain (string)
     nearestStormBearing : -99 (real)
     nearestStormBearing.ui : --  (string)
     nearestStormBearingIcon : -99 (real)
     nearestStormDistance : 0 (real)
     nearestStormDistance.ui : 0 mi. (string)
     nearestStormDistanceIcon : 0 (real)
     onOffState : false (bool)
     onOffState.ui : Disabled (string)
     ozone : 313.42 (real)
     ozone.ui : 313  (string)
     ozoneIcon : 313 (real)
     precipIntensity : 0.1315 (real)
     precipIntensity.ui : 0.13 in. (string)
     precipIntensityIcon : 0 (real)
     precipProbability : 100 (real)
     precipProbability.ui : 100% (string)
     precipProbabilityIcon : 100 (real)
     pressure : 1009.6 (real)
     pressure.ui : 1010 mb (string)
     pressureIcon : 1010 (real)
     summary : Rain (string)
     temperature : 68.48 (real)
     temperature.ui : 68° (string)
     temperatureIcon : 68 (real)
     uv : 0 (real)
     uv.ui : 0  (string)
     uvIcon : 0 (real)
     visibility : 6.54 (real)
     visibility.ui : 7 mi. (string)
     visibilityIcon : 7 (real)
     weatherSummaryEmailSent : true (bool)
     windBearing : 40 (real)
     windBearing.ui : 40 (string)
     windBearingIcon : 40 (real)
     windBearingName : Northeast (string)
     windGust : 17.87 (real)
     windGust.ui : 18 mph (string)
     windGustIcon : 18 (real)
     windSpeed : 9.44 (real)
     windSpeed.ui : 9 mph (string)
     windSpeedIcon : 9 (real)

There's lots of online tutorials to learn Python scripting--that's how I learned (with the help of folks here)--the Indigo documentation shows how to apply those concepts to Indigo. It may seem like a lot to absorb, but if you take it simple at first, watch some online tutorials, and ramp up your skills bit by bit, you'll eventually find that there's almost nothing you can't do in Indigo.

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

[My Plugins] - [My Forums]

Posted on
Fri Aug 24, 2018 8:48 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Starting out with Python

hamw wrote:
Jay, here is a link to the other thread. I looked in the referenced tutorial but didn’t see anything about how to write conditionals. Would be great to have a few examples. Appreciate your help.

viewtopic.php?f=107&t=19933&hilit=Conditional


If you're talking about scripts in the Conditions tab, those can only be AppleScripts at the moment (we will change that in the same release in which we remove AppleScript).

If you want to do it now, then you would just remove the condition script from the Conditions tab (change it to Always) and make your action a Python script. Then the pattern for that script would be something like this:

Code: Select all
# Perform your conditional logic here
if condition_logic_result:
    indigo.actionGroup.execute(ID_OF_TRUE_ACTION_GROUP_HERE)
else:
    # you have the option of doing something here if the condition logic is false
    indigo.actionGroup.execute(ID_OF_FALSE_ACTION_GROUP_HERE)


This is, in fact, a somewhat more flexible approach since you have the option of executing any number of actions (via action groups) depending on what your conditional logic decides.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Sep 21, 2018 4:37 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Starting out with Python

Here"s another ... I'd like to call a powerlinc group from Python as I have found the system to be a lot more responsive with these vs action groups.

this code works.
Code: Select all
if value of variable "LightsFamRmScenes" is "4" then
      send insteon group instnTurnOn using index 4
   end if


in pseudo python:

Code: Select all
LightsFamRmScenes = indigo.variables[77625543].value
indigo.server.log(u"{0}".format(LightsFamRmScenes))

## I want to say... "if LightsFamRmScenes = "4" then send group 4"

LightsFamRmScenes = "4"

kInstnGroupCommand.group (4)


The script returns this error:

embedded script, line 9, at top level
NameError: global name 'kInstnGroupCommand' is not defined

deleting the "k" in the front does not work either.

I got the "kInstnGroupCommand" from here: https://wiki.indigodomo.com/doku.php?id ... on_class&s[]=insteon%20group#insteon_send_group_command_enumeration

Thanks!

Posted on
Fri Sep 21, 2018 4:44 pm
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

Re: Starting out with Python

try (untested):
Code: Select all
LightsFamRmScenes = indigo.variables[77625543].value
if LightsFamRmScenes == "4":
    indigo.insteon.sendSceneOn(int(LightsFamRmScenes))   # or just pass 4 in as the argument?

Image

Posted on
Sun Sep 23, 2018 12:29 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Starting out with Python

Thanks for the help on the script above ... did the trick! Any thoughts on this one? AFAIK the syntax is correct but it is throwing an error.

Code: Select all
Lights_Driveway_Stay_On = indigo.variables[96354276]
BackYardLightsStayON = indigo.variables[688132873]

if Lights_Driveway_Stay_On.value == “true”:
   indigo.variable.updateValue(Lights_Driveway_Stay_On, “false”)
   indigo.variable.updateValue(BackYardLightsStayON, “false”)

else
   indigo.variable.updateValue(Lights_Driveway_Stay_On, “true”)
   indigo.variable.updateValue(BackYardLightsStayON, “true”)


Script Error embedded script: invalid syntax
Script Error around line 5 - "if Lights_Driveway_Stay_On.value == “true”:

Posted on
Sun Sep 23, 2018 1:14 pm
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Starting out with Python

Your quotes are handed not ordinary as in "

Also you are missing a
:
after the else

Revised code:
Code: Select all
Lights_Driveway_Stay_On = indigo.variables[96354276]
BackYardLightsStayON = indigo.variables[688132873]
if Lights_Driveway_Stay_On.value == "true":
    indigo.variable.updateValue(Lights_Driveway_Stay_On, "false")
    indigo.variable.updateValue(BackYardLightsStayON, "false")
else:
    indigo.variable.updateValue(Lights_Driveway_Stay_On, "true")
    indigo.variable.updateValue(BackYardLightsStayON, "true")

:)

Posted on
Mon Sep 24, 2018 6:24 am
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Starting out with Python

Jon, thanks! Did not realize how particular Python was!

Re the post I had here, in poking around I found a more appropriate thread and moved it there.

Ham

Who is online

Users browsing this forum: No registered users and 3 guests