Page 1 of 1

Python script error

PostPosted: Wed Oct 17, 2018 8:21 am
by ken_jacobs
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

Re: Python script error

PostPosted: Wed Oct 17, 2018 8:49 am
by jay (support)
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).

Re: Python script error

PostPosted: Thu Oct 18, 2018 9:36 am
by ken_jacobs
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.

Re: Python script error

PostPosted: Thu Oct 18, 2018 10:12 am
by ken_jacobs
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.

Re: Python script error

PostPosted: Thu Oct 18, 2018 11:11 am
by CliveS
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)

Re: Python script error

PostPosted: Thu Oct 18, 2018 12:41 pm
by ken_jacobs
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.

Re: Python script error

PostPosted: Thu Oct 18, 2018 12:53 pm
by ken_jacobs
Zwave ge dimmer controller, mounted in electrical box instead of normal light switch.

Re: Python script error

PostPosted: Thu Oct 18, 2018 1:36 pm
by ken_jacobs
That is an excellent suggestion, thanks.

Re: Python script error

PostPosted: Thu Oct 18, 2018 4:15 pm
by jay (support)
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"