Script to toss noisy sensor value

Posted on
Sat Jun 18, 2022 5:59 pm
SMUSEBY offline
Posts: 503
Joined: Sep 16, 2009
Location: California

Script to toss noisy sensor value

I'm using a phidgets ambient light sensor to trigger lights on/off, which works most of the time. But too often, in the middle of the day or night, the lights come on when they shouldn't. I suspect there is a noisy reading which triggers it's now dark action groups.
The following script will hopefully compare two ambient light values (separated by 5 seconds), and if they're the same, proceed with the action groups; otherwise, ignore the false value.
Code: Select all
import time
varA = indigo.variables[714999465].getValue(int) # ambLt_0
varB = indigo.variables[911378874].getValue(int) # ambLt_1
varD = indigo.variables[602276231].getValue(bool) # ambLtValid

indigo.trigger.enable(913722846, False) #disable trigger while checking

dev = indigo.devices[1666063006] # amb light sensor (state)
ambLt = int(dev.states["state"])
varA = ambLt # initial value

time.sleep(5) #delay to allow possibly aberrant reading to be ignored
ambLt = int(dev.states["state"])
varB = ambLt # second value

if varA == varB:
   indigo.variable.updateValue(602276231, "True")
else:
 indigo.variable.updateValue(602276231, "False")
 
indigo.trigger.enable(913722846, True) #enable enable trigger


My question is that while the script runs, I'm worried that the line with "=="
Code: Select all
if varA == varB:

is looking at string values, not integer values.
Also, any thoughts how the script could be improved?

Posted on
Sat Jun 18, 2022 10:16 pm
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Script to toss noisy sensor value

Firstly apologies that I’m on iPhone so I’m paraphrasing some code.

To answer your question, the == line is correctly comparing integers as you’ve assigned both of them an int(state). You could combine the two assignment lines:

Code: Select all
ambLt = int(state)
varA = ambLt
into one line:

Code: Select all
varA = int(state)
Re your approach I’m not immediately computing how/where you’re putting this code to achieve what you want; allow me to offer a possible alternative, in pseudocode:

2 variables:
lastAmbLtLevel to store previous value (that might be noisy)
trueAmbLtLevel to store value you’ve confirmed isn’t noisy

Trigger on device update received
Code: Select all
compare new state value with lastAmbLtLevel
if(it’s the same or near enough):
    Accept it is true
    insert new state to trueAmbLtLevel
else(it’s noisy)
    Don’t do anything

#Regardless, store value for next comparison:
insert new state to variable lastAmbLtLevel
Trigger on variable trueAmbLtLevel changing
Code: Select all
Turn lights on/off accordingly
I can’t preview this post on iPhone before submitting so if you’re seeing it arrive by notification, be aware I might edit straight away.


Sent from my iPhone using Tapatalk Pro

Posted on
Sun Jun 19, 2022 6:06 am
FlyingDiver offline
User avatar
Posts: 7184
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Script to toss noisy sensor value

You'll have to repeat the retrieval of the phidget device again after the sleep, as the copy you got won't have the updated value. That's this line:

Code: Select all
dev = indigo.devices[1666063006] # amb light sensor (state)


But I think you really need to break this into two scripts - one the does everything before the sleep, then another that does the part after. And then use the Indigo delay mechanism to run the second script. Otherwise, this will lock up large parts of your Indigo system for 5 seconds every time this script fires.

OTOH, does it really need to be 5 seconds? How often does the phidget device update?

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Jun 19, 2022 12:41 pm
SMUSEBY offline
Posts: 503
Joined: Sep 16, 2009
Location: California

Re: Script to toss noisy sensor value

5 seconds is a placeholder
Actually, I'm afraid I can't think of an easy way to solve this noisy sensor problem. There are 2 components: the first is to verify a new sensor value, and the second is to use the accepted value to trigger the devices.
The script with adjustments solves the first problem.
What I can't figure out is how to use the verified values. The sensor updates the value when it detects a change - so if an airplane casts a shadow, there could be 3 readings in less than a second. Every time there is a change in the ambient light value, it will trigger the script which takes n seconds to test the change. The problem is that the device on/off triggers use the same change in ambient light value. To make this work, there needs to be a delay in these triggers to allow for the this script to make its assessment.
I don't know how to delay a trigger. My clumsy solution is to have the device triggers be in a script that would run after the verification script has had time to do its cleansing. But I gather that ties up Indigo.
There must be a better way. Suggestions?

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests