Page 1 of 1

Compare a variable to a sensor to fire a trigger

PostPosted: Sat Mar 30, 2019 4:29 pm
by Gangotti
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?

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

PostPosted: Sun Mar 31, 2019 7:31 am
by jalves
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.....

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

PostPosted: Sun Mar 31, 2019 12:14 pm
by Gangotti
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 ?

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

PostPosted: Sun Mar 31, 2019 2:07 pm
by jalves
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)

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

PostPosted: Mon Apr 01, 2019 5:23 pm
by jay (support)
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

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

PostPosted: Sun Apr 28, 2019 5:19 pm
by Gangotti
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

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

PostPosted: Mon Apr 29, 2019 10:16 am
by jay (support)
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).

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

PostPosted: Mon Apr 29, 2019 9:00 pm
by Gangotti
Jay Thanks for this!