Getting Device States in Python

Posted on
Wed Nov 07, 2018 5:23 pm
teejay6 offline
Posts: 44
Joined: Jan 31, 2011
Location: Menlo Park, CA

Getting Device States in Python

I am trying to get the initial colorTemp device state for a Philips color hue bulb, change the colorTemp via script, and then return colorTemp to the initial value. I am able to change the colorTemp, but the device state value I get from bulb_name.states['colorTemp'] does not change.

Here is a script that illustrates the problem. I set the initial colorTemp to 2967. The initial bulb_name.states['colorTemp'] value = 2967. This is correct. (In fact, I get the correct bulb_name.states['colorTemp'] value on the first request every time, whether it is 2967 or any other value.)
I then set the colorTemp value to 2237. The bulb changes to 2237. The colorTemp value in the Indigo 7 app Device List updates and shows 2237. But the bulb_name.states['colorTemp'] value is still = 2967.
I then set the colorTemp value to 6410. The bulb changes to 6410. The colorTemp value in the Indigo 7 app Device List updates and shows 6410. But the bulb_name.states['colorTemp'] value is still = 2967.
I then set the colorTemp value back to 2237. The bulb changes to 2237. The colorTemp value in the Indigo 7 app Device List updates and shows 2237. But the bulb_name.states['colorTemp'] value is still = 2967.

Here is the script I have been using:
Code: Select all
#Script to get current state of light bulb device

import time

delay_margin = 5
bulb_name = indigo.devices[345799647]

#Initial values for color temp
color_temp_initial_var = 2237
indigo.server.log("Color Temperature Initial Variable is: " + str(color_temp_initial_var))

indigo.dimmer.setColorLevels(bulb_name, 100, 100, 100, 100, 0, int(color_temp_initial_var))

time.sleep(delay_margin)
indigo.server.log("Delay in seconds: " + str(delay_margin))

#Get Initial State Values
color_temp_state = bulb_name.states['colorTemp']
indigo.server.log("Color Temperature Device State is: " + str(color_temp_state))

time.sleep(delay_margin)
indigo.server.log("Delay: " + str(delay_margin))

#Set New values for color temp
color_temp_new_var = 6410
indigo.server.log("Variable Color Temperature is: " + str(color_temp_new_var))

indigo.dimmer.setColorLevels(bulb_name, 100, 100, 100, 100, 0, int(color_temp_new_var))
   
time.sleep(delay_margin)
indigo.server.log("Delay in seconds: " + str(delay_margin))
   
#Get New State Values
indigo.server.log("Color Temperature Device State is: " + str(color_temp_state))

time.sleep(delay_margin)
indigo.server.log("Delay in seconds: " + str(delay_margin))

#Set Back to Initial values for color temp
indigo.server.log("Variable Color Temperature Initial is: " + str(color_temp_initial_var))

indigo.dimmer.setColorLevels(bulb_name, 100, 100, 100, 100, 0, int(color_temp_initial_var))

time.sleep(delay_margin)
indigo.server.log("Delay in seconds: " + str(delay_margin))

#Get State Values for Initial color_temp variable
indigo.server.log("Color Temperature Device State is: " + str(color_temp_state))
indigo.server.log("Script Complete")


Here is the log output:

Action Group - Python Test 3
Script - Color Temperature Initial Variable is: 2237
Sent Hue Lights - "Lights - Office Table Lamp Left - Hue7C" on to 100 using color temperature 2237.0 K at ramp rate 0.5 sec.
Script - Delay in seconds: 5
Script - Color Temperature Device State is: 2967
Script - Delay: 5
Script - Variable Color Temperature is: 6410
Sent Hue Lights - "Lights - Office Table Lamp Left - Hue7C" on to 100 using color temperature 6410.0 K at ramp rate 0.5 sec.
Script - Delay in seconds: 5
Script - Color Temperature Device State is: 2967
Script - Delay in seconds: 5
Script - Variable Color Temperature Initial is: 2237
Sent Hue Lights - "Lights - Office Table Lamp Left - Hue7C" on to 100 using color temperature 2237.0 K at ramp rate 0.5 sec.
Script - Delay in seconds: 5
Script - Color Temperature Device State is: 2967
Script - Script Complete


Is there something else I need to do to get the correct device state of the colorTemp (or any of the values) after I change them in a python script? Thanks for any help.

Posted on
Wed Nov 07, 2018 5:29 pm
kw123 offline
User avatar
Posts: 8333
Joined: May 12, 2013
Location: Dallas, TX

Re: Getting Device States in Python

this
Code: Select all
color_temp_state = bulb_name.states['colorTemp']
gets a copy
you need to do a refresh if you want to see the new values .. just do it again

Karl

Posted on
Wed Nov 07, 2018 6:26 pm
teejay6 offline
Posts: 44
Joined: Jan 31, 2011
Location: Menlo Park, CA

Re: Getting Device States in Python

Thanks! I added the refresh after each variable change, and it worked like a charm. I actually had a problem with the variable name I was using. I changed it to the following, and it worked better:
Code: Select all
bulb_name = 345799647
color_temp_state = indigo.devices[int(bulb_name)].states['colorTemp']

Posted on
Wed Nov 07, 2018 6:56 pm
kw123 offline
User avatar
Posts: 8333
Joined: May 12, 2013
Location: Dallas, TX

Re: Getting Device States in Python

Just a remark. Int() requires a number not a name.


Sent from my iPhone using Tapatalk

Posted on
Wed Nov 07, 2018 10:16 pm
teejay6 offline
Posts: 44
Joined: Jan 31, 2011
Location: Menlo Park, CA

Re: Getting Device States in Python

OK, Thanks.

Posted on
Sun Aug 30, 2020 1:45 pm
wideglidejrp offline
User avatar
Posts: 555
Joined: Jan 15, 2012
Location: Danbury, CT

Re: Getting Device States in Python

How can I make a list of all my devices and whether each is on or off? Something like the following...

Code: Select all
for dev in indigo.devices.itervalues():
    indigo.server.log(dev.name, dev.onState)

John R Patrick
Author of
Home Attitude

Posted on
Mon Aug 31, 2020 6:44 pm
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

Re: Getting Device States in Python

Try:

Code: Select all
for dev in indigo.devices.iter("indigo.sensor, indigo.relay, indigo.dimmer"):
    indigo.server.log(dev.name + u": " + unicode(dev.onState))

Image

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 7 guests