Python script error

Posted on
Wed Oct 17, 2018 8:21 am
ken_jacobs offline
Posts: 187
Joined: Jul 23, 2005

Python script error

I am trying to test the front hall light controller state as a condition with the following:
lamp = iindigo.devices[1867604195] # "Front Hall Light"
if hasattr(lamp, 'onState'):

and I get the following error:
Error script error: around characters 7 to 15
Error script error: A unknown token can’t go after this identifier. (-2740)
I am an AppleScript guy trying to convert to Python, so help is needed.

Thanks

Posted on
Wed Oct 17, 2018 8:49 am
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python script error

When you post a nonfunctioning script, we need to see the whole script please (and put it inside a code block so we can see the formatting which is important in Python).

I do see one syntax error, indigo.devices has one i not two:

Code: Select all
lamp = indigo.devices[1867604195] # "Front Hall Light"


But without the whole script it's hard to know what else is wrong. And a more complete description of what you're trying to do would also help.

And make sure you have Python selected as the script type, not AppleScript (for embedded scripts).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Oct 18, 2018 9:36 am
ken_jacobs offline
Posts: 187
Joined: Jul 23, 2005

Re: Python script error

OK,

I am attempting to test the controller state for my porch light. I currently have an AppleScript embedded script that checks the porch light periodically in the evening to see if a power outage has occurred since the GE controller will drop off with a power loss. If loss has happened. I turn outside lights back on. The test script is:

if hasattr(indigo.device(1678710193), 'onState'):
indigo.server.log("Porch Light on")
endif


I get this error:
embedded script, line 1, at top level
TypeError: 'DeviceCmds' object is not callable

I call check the onState of the porch light in AppleScript.

Posted on
Thu Oct 18, 2018 10:12 am
ken_jacobs offline
Posts: 187
Joined: Jul 23, 2005

Re: Python script error

I now poll the front porch light every half hour during evening using an embedded AppleScript. Although the indigo iMac is on a ups and I also have a whole house generator, I awill still lose my outside lights during the power transition between generator and normal power. The AppleScript works great, I was trying to find an equivalent Python script, given that the next version of Indigo will not support AppleScript.

Posted on
Thu Oct 18, 2018 11:11 am
CliveS offline
Posts: 770
Joined: Jan 10, 2016
Location: Medomsley, County Durham, UK

Re: Python script error

ken_jacobs wrote:
I now poll the front porch light every half hour during evening using an embedded AppleScript. Although the indigo iMac is on a ups and I also have a whole house generator, I awill still lose my outside lights during the power transition between generator and normal power. The AppleScript works great, I was trying to find an equivalent Python script, given that the next version of Indigo will not support AppleScript.


Try the following

Code: Select all
lamp = indigo.devices[indigo.devices[1678710193] # "Porch Lights"

if not lamp.onState:
   
   indigo.server.log("Porch Light Off So Turn On")
   indigo.device.turnOn(lamp)

CliveS

Indigo 2023.2.0 : macOS Ventura 13.6.3 : Mac Mini M2 : 8‑core CPU and 10‑core GPU : 8 GB : 256GB SSD
----------------------------------------------------------------------------------
The best way to get the right answer on the Internet is not to ask a question, it's to post the wrong answer

Posted on
Thu Oct 18, 2018 12:41 pm
ken_jacobs offline
Posts: 187
Joined: Jul 23, 2005

Re: Python script error

I could not get your suggestion to run, problem with if statement. Here is my AppleScript script:

Code: Select all
if value of variable "Evening" = "true" then
   status request of device "Front Porch Light"
   if on state of device "Front Porch Light" is false then
      log "Front Porch Light status is off"
      delay 2
      turn on device "Front Porch Light"
      turn on device "Side Light, Large"
      turn on device "Deck, Light"
   end if
else
   status request of device "Front Porch Light"
   if on state of device "Front Porch Light" is true then
      log "Front Porch Light status is on"
      execute group "Turn Outside Lights Off"
   end if
end if

Notice that I have to make a status inquiry of the device since indigo is holding the wrong device state after a power outage.

Posted on
Thu Oct 18, 2018 12:53 pm
ken_jacobs offline
Posts: 187
Joined: Jul 23, 2005

Re: Python script error

Zwave ge dimmer controller, mounted in electrical box instead of normal light switch.

Posted on
Thu Oct 18, 2018 1:36 pm
ken_jacobs offline
Posts: 187
Joined: Jul 23, 2005

Re: Python script error

That is an excellent suggestion, thanks.

Posted on
Thu Oct 18, 2018 4:15 pm
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python script error

For completeness, here's the script above converted to Python:

Code: Select all
indigo.device.statusRequest(1678710193)
indigo.script.sleep(5) # give the module time to respond (or fail)

# Get the lamp instance
lamp = indigo.devices[1678710193] # "Porch Lights"
# Get the variable value
is_evening = indigo.variables[123].getValue(bool) # ID of variable "Evening"
if is_evening:
    if not lamp.onState:
        indigo.server.log("Front Porch Light status is off")
        indigo.script.sleep(2) # not sure why this delay is here, may not really be necessary
        indigo.device.turnOn(lamp)
        indigo.device.turnOn(456) # ID of "Side Light, Large"
        indigo.device.turnOn(789) # ID of "Deck, Light"
else:
    if lamp.onState:
        indigo.server.log("Front Porch Light status is on")
        indigo.actionGroup.execute(987) # ID of action group "Turn Outside Lights Off"

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest