Getting the sub-device data

Discuss Python scripts here.
User avatar
Different Computers
Posts: 2609
Joined: Sat Jan 02, 2016 10:07 am
Location: East Coast
Contact:

Getting the sub-device data

Post by Different Computers »

I'm trying to get the Luminance data from some motion sensors, and can't find the documentation to pull that specific data. In the scripting shell I can

Code: Select all

print(device.name, device.id, device.deviceTypeId)
and that tells me (for example)

Code: Select all

Front Porch Luminance 1757368923 zwValueSensorType 
but then when I put "zwValueSensorType" in a script:

Code: Select all

# Define a list of device IDs to average
device_ids = [1757368923, 1128085779, 454347936]

# Get the current luminance value for each device and add it to a list
luminance_values = []
for device_id in device_ids:
    device = indigo.devices[device_id]
    luminance_value = float(device.states.get("zwValueSensorType", 0))
    if luminance_value > 0:
        # Convert lux values to a percentage scale (assuming max value of 900)
        if device_id == 454347936:
            percentage_value = (luminance_value / 9) if luminance_value <= 900 else 100.0
            luminance_values.append(percentage_value)
        else:
            luminance_values.append(luminance_value)
    else:
        indigo.server.log(f"Device {device.name} ({device.id}) has no zwValueSensor state value")

# Calculate the average luminance value
if len(luminance_values) > 0:
    average_luminance = sum(luminance_values) / len(luminance_values)
else:
    average_luminance = 0

# Set the value of an existing Indigo variable with the average luminance
variable_name = "AverageLuminance"
variable = indigo.variables[variable_name]
if variable is not None:
    variable.value = str(average_luminance)
    indigo.server.log(f"Set value of variable '{variable_name}' to {average_luminance}")
else:
    indigo.server.log(f"Variable '{variable_name}' not found")


I get "Device Front Porch Luminance (1757368923) has no zwValueSensor state value" so I suppose I don't know how to ask Indigo for what I really want. How do I make the script grab the sub-device sensor value? I checked the scripting tutorial but did not see a method for doing this.
Sonoma on a Mac Mini M1 running Airfoil Pro, Bond Home, Camect, Roku Network Remote, Hue Lights, DomoPad, Adapters, Home Assistant Agent, HomeKitLinkSiri, EPS Smart Dimmer, Fantastic Weather, Nanoleaf, LED Simple Effects, Grafana. UnifiAP
User avatar
matt (support)
Site Admin
Posts: 21476
Joined: Mon Jan 27, 2003 1:17 pm
Location: Texas
Contact:

Re: Getting the sub-device data

Post by matt (support) »

To see what states are defined by the device right-click on it and choose the Print Device Details to Event Log menu item and search for the states dict. For a sensor device it will look like this:

Code: Select all

states : States : (dict)
     sensorValue : 87.300000 (real)
     sensorValue.ui : 87.3 °F (string)
So in this case the state you want to use on your luminance_value = ... line is not zwValueSensorType (which is the device type ID), but instead just sensorValue.

Note sensor devices also have an attribute that mirrors the state, so you could use that directly: device. sensorValue
Image
User avatar
Different Computers
Posts: 2609
Joined: Sat Jan 02, 2016 10:07 am
Location: East Coast
Contact:

Re: Getting the sub-device data

Post by Different Computers »

matt (support) wrote: Note sensor devices also have an attribute that mirrors the state, so you could use that directly: device. sensorValue
Thanks! when I tried device.sensorValue that didn't work, but sensorValue did. Much appreciated!
Sonoma on a Mac Mini M1 running Airfoil Pro, Bond Home, Camect, Roku Network Remote, Hue Lights, DomoPad, Adapters, Home Assistant Agent, HomeKitLinkSiri, EPS Smart Dimmer, Fantastic Weather, Nanoleaf, LED Simple Effects, Grafana. UnifiAP
Post Reply

Return to “Python Scripting”