Compare a variable to a sensor to fire a trigger

Posted on
Sat Mar 30, 2019 4:29 pm
Gangotti offline
Posts: 29
Joined: Dec 23, 2014

Compare a variable to a sensor to fire a trigger

I'm trying to find a way to automate the ceiling fan in my bedroom. I already have the fan running on indigo and can control it through the hue bridge, and I have and Aeotec multisensor 6 in the room monitoring temperature. I would like make a way that I can set a variable for each fan setting (low, medium, and high) and if the temperature goes above or below that set point it changes the speed of the fan automatically by triggering the fan to the correct speed. Additionally there would be a place in the control page where the user can press an icon that will increase or decrease the set point for each speed. Is there a way to do this in indigo, or is this going to have to be a python script?

Posted on
Sun Mar 31, 2019 7:31 am
jalves offline
Posts: 523
Joined: Jun 16, 2013

Re: Compare a variable to a sensor to fire a trigger

I believe you can do this within Indigo. I had a somewhat similar need with my bathroom exhaust fans and Matt&Jay offered this script that turns the exhaust fans on when the bathroom humidity is high.

Code: Select all
# Get the two humidity values
bathHumidity = indigo.devices[288098054].sensorValue  # ID of bathroom humidity device
bedroomHumidity = indigo.devices[1185510746].sensorValue  # ID of bedroom humidity device

# Calculate the difference and check to see if it's greater than or equal to 20
if (bathHumidity - bedroomHumidity) >= 20:
   # Difference is greater than or equal to 20, so turn on the device(s) for 5 minutes
   indigo.device.turnOn(404137540, duration=300)  # ID of exhaust fan
   indigo.device.turnOn(116176458, duration=300) #ID of toilet fan


The scrip runs as an action in a Indigo trigger which fires every time the bathroom Multi-sensor humidity value changes. You could use this same approach for your overhead fans.

Hmmm, now that I've written all that out, I just might try something like that for my fans.....

Running Indigo 7.3.1 on a 27" iMac (mid-2017), 40G memory, 1T Fusion Drive, OS X 10.14.4
Jeff

Posted on
Sun Mar 31, 2019 12:14 pm
Gangotti offline
Posts: 29
Joined: Dec 23, 2014

Re: Compare a variable to a sensor to fire a trigger

ill give it a try. I will say, I have not done a lot of scripting (its on the list to learn) I only have a question
When turning on the device, yours has it running for 300 seconds? how do i write it to stay on all the time ?

Posted on
Sun Mar 31, 2019 2:07 pm
jalves offline
Posts: 523
Joined: Jun 16, 2013

Re: Compare a variable to a sensor to fire a trigger

Just leave the duration part out. So it would say something like
Code: Select all
indigo.device.turnOn(404137540)  # ID of fan


Of course that will turn the fan on high (100%). Not sure how to tell it to set to medium or low. To turn it off it would be indigo.device.turnOff(device ID here)

Running Indigo 7.3.1 on a 27" iMac (mid-2017), 40G memory, 1T Fusion Drive, OS X 10.14.4
Jeff

Posted on
Mon Apr 01, 2019 5:23 pm
jay (support) offline
Site Admin
User avatar
Posts: 15543
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Compare a variable to a sensor to fire a trigger

Code: Select all
indigo.speedcontrol.setSpeedIndex(123, value=0)  # turn off
indigo.speedcontrol.setSpeedIndex(123, value=1)  # low
indigo.speedcontrol.setSpeedIndex(123, value=2)  # medium
indigo.speedcontrol.setSpeedIndex(123, value=3)  # high

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Apr 28, 2019 5:19 pm
Gangotti offline
Posts: 29
Joined: Dec 23, 2014

Re: Compare a variable to a sensor to fire a trigger

So Happy news is I got my first script to run without errors, the only problem is It is not exactly running correctly. how do I write the result of a function to a variable so I can make sure the function is doing what it is supposed to be doing?

Here is what I have scripted so far, the idea is I have an Aeotec multisensor in the room monitoring temperature and I have "Dummy"x10 device that acts as a way to define a setpoint for the speed of the fan. I then have a trigger that fires if there is any change to the temperature sensor. The action is the script which is basically subtracting the temperature of the room from the setpoint device. if the number returned is greater than 0 it turns the fan to High, if it is less than 0 it turns the fan to medium. I will eventually add 2 more setpoints for Med and Low, but I want to make this work first. I am, open to any suggestions here as this is my first real attempt at scripting.
Code: Select all
   # Get the temperature value from the sensor and get the setpoint value from the setpoint device.
RoomTemp = indigo.devices[1770450782].sensorValue # ID of the Temperature device
RoomHighSetpoint = indigo.devices[1254352989].brightness # ID of the variable setpoint

   #calculate the difference and check to see if it is greater or equal to 0
if (RoomTemp - RoomHighSetpoint) > 0: # Difference is greater than 0, so turn on the device
   indigo.speedcontrol.setSpeedIndex(731663956, value=3) # turns fan to High
if (RoomTemp - RoomHighSetpoint) > 0: # Difference is greater than 0, so turn on the device
   indigo.speedcontrol.setSpeedIndex(731663956, value=2) # turns fan to Med

Posted on
Mon Apr 29, 2019 10:16 am
jay (support) offline
Site Admin
User avatar
Posts: 15543
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Compare a variable to a sensor to fire a trigger

First, notice that your two IF blocks are identical (both are looking to see if the calculation is > 0, so both will fire):

Code: Select all
if (RoomTemp - RoomHighSetpoint) > 0: # Difference is greater than 0, so turn on the device
   indigo.speedcontrol.setSpeedIndex(731663956, value=3) # turns fan to High
if (RoomTemp - RoomHighSetpoint) > 0: # Difference is greater than 0, so turn on the device
    indigo.speedcontrol.setSpeedIndex(731663956, value=2) # turns fan to Med


So it will only set to Medium if the difference is > 0 (because the first IF sets it to High, but then the second identical test sets it to Medium). I think that's likely just a copy/paste error - you forgot to change it to less than.

Here's a tweaked script that will log the results of your calculations to the Event Log window and will avoid the double IF problem which was somewhat obscuring what the problem was:

Code: Select all
# Get the temperature value from the sensor and get the setpoint value from the setpoint device.
RoomTemp = indigo.devices[1770450782].sensorValue # ID of the Temperature device
RoomHighSetpoint = indigo.devices[1254352989].brightness # ID of the variable setpoint

# Log the values and the calculation results:
calculation_result = RoomTemp - RoomHighSetpoint
indigo.server.log("{} - {} = {}".format(RoomTemp, RoomHighSetpoint,  calculation_result))

#calculate the difference and check to see if it is greater or equal to 0
if (calculation_result) > 0: # Difference is greater than 0, so turn on the device
    indigo.speedcontrol.setSpeedIndex(731663956, value=3) # turns fan to High
elif (calculation_result) < 0: # Difference is LESS than 0, so turn on the device
    indigo.speedcontrol.setSpeedIndex(731663956, value=2) # turns fan to Med


Not tested, but it should be close. There is a bit of a hole in your logic though - if the value is exactly 0 nothing happens (maybe that's where you turn the fan off in another elif block).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Apr 29, 2019 9:00 pm
Gangotti offline
Posts: 29
Joined: Dec 23, 2014

Re: Compare a variable to a sensor to fire a trigger

Jay Thanks for this!

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 0 guests