Need help w/ condition script or alternative suggestion

Posted on
Sun Jun 05, 2016 3:23 am
zama36 offline
Posts: 6
Joined: Mar 23, 2016
Location: Washington

Need help w/ condition script or alternative suggestion

So here's what I'm attempting to do. Set up a trigger to turn on a house fan if all following are true.

    House temp greater than 70.
    Outside temp at least 5 degrees cooler.
    Window A open.
    Window B open.

I'm using Wunderground for the outside temp. In the standard rules I can access "Current Conditions - Temperature." How do I access it from an AppleScript? There is no device property that syncs up with the temperature. Here's the portion of the script setting variables.

Code: Select all
set HouseTemp to temperatures of device named "Dining Room Thermostat (Nest)"
log HouseTemp
#set OutsideTemp to temperatures of device named "Temperature"
#log OutsideTemp
set DiffTemp to HouseTemp #-OutsideTemp
log DiffTemp

I have looked at possibly using Wunderground's API. However, considering they require a website address when registering and I don't have one since this is for personal use, I am leery of going through all the registration process, scripting and configuration only to have WU pull my API key. Thoughts?

Posted on
Sun Jun 05, 2016 4:51 am
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Need help w/ condition script or alternative suggestion

Unless things have changed, I believe that it's impossible to access custom device states from plugins using Applescript. However, what you want to do is accomplished very easily using Python. There's certainly more than one way to do it, but the following code should be very close and give you a jumping off point. Of course, you'll need to replace my device numbers with your own.

Code: Select all
house_fan = 1642278706
house_temperature = float(indigo.devices[1700355009].states['temperatureInput1'])
outside_temperature = float(indigo.devices[1258163860].states['temp'])
window_a = indigo.devices[380797747].states['onOffState']
window_b = indigo.devices[380797747].states['onOffState']

if house_temperature > 70:
    if (house_temperature - outside_temperature) >= 5.0:
        if window_a == 'on' and window_b == 'on':
            indigo.device.turnOn(house_fan)
else:
    indigo.device.turnOff(house_fan)

The code is effectively untested (I did make sure it would compile) so if you run into any snags please post back.
Dave

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Jun 05, 2016 10:15 am
zama36 offline
Posts: 6
Joined: Mar 23, 2016
Location: Washington

Re: Need help w/ condition script or alternative suggestion

Thanks for the help. I really appreciate it.

I was able to set up a new trigger based on house temp above 70 with an action of running an embedded script.

I slightly changed what you suggested to ensure that I was actually getting values and logged them to the event viewer. It seems like it isn't walking the if statements. Any thoughts?

Code: Select all
house_temperature = float (indigo.devices[678652363].states['temperatureInput1'])
indigo.server.log("House is " + str(house_temperature))
outside_temperature = float(indigo.devices[126958715].states['temp'])
indigo.server.log("Outside is " + str(outside_temperature))
diff_temperature = house_temperature - outside_temperature
indigo.server.log("Difference is " + str(diff_temperature))
window_a = indigo.devices[1657238355].states['onOffState']
indigo.server.log("Device " + str(window_a))

if house_temperature > 70:
   if diff_temperature >= 4.0:
      if window_a == 'True':
         indigo.device.turnOn(1104590197)
else:
    indigo.device.turnOff(1104590197)


Jun 5, 2016, 8:52:34 AM
Script House is 74.0
Script Outside is 69.8
Script Difference is 4.2
Script Device True

Posted on
Sun Jun 05, 2016 10:55 am
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Need help w/ condition script or alternative suggestion

Modify your script so that you can determine for sure if the "if" statements are not getting raised:

Code: Select all

if house_temperature > 70:
   if diff_temperature >= 4.0:
      if window_a == 'True':
         indigo.server.log("If statement reached")
         indigo.device.turnOn(1104590197)
else:
    indigo.server.log("else statement reached")
    indigo.device.turnOff(1104590197)



Also, you can simply put:
Code: Select all
if diff_temperature >= 4:


Rather than:
Code: Select all
if diff_temperature >= 4.0:

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Sun Jun 05, 2016 10:58 am
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Need help w/ condition script or alternative suggestion

Nevermind, here is your problem:

Code: Select all
if window_a == 'True':

should be
Code: Select all
if window_a == True:

alternately:
Code: Select all
if window_a:
Boolean values are True and False, not 'True' and 'False' unless you unicode them to be strings. By just saying "if window_a" will equate to "is true", the "==True" is somewhat redundant.

Your condition code can be any one of the following and should raise the IF condition:
Code: Select all
if window_a:
         indigo.device.turnOn(1104590197)

if str(window_a) == 'True':
         indigo.device.turnOn(1104590197)

if window_a == True:
         indigo.device.turnOn(1104590197)


Since you are presumably optimizing this code to work on multiple windows you can simplify it with this code:
Code: Select all
thermostat = indigo.devices[678652363]
weather = indigo.devices[126958715]
window =  indigo.devices[1657238355]
tempdiff = weather.states['temp'] - thermostat.states["temperatureInput1"] # Both of these states are already floats, you shouldn't have to convert them

if thermostat.states["temperatureInput1"] > 70 and tempdiff >= 4 and window_a.states["onOffState"]:
    indigo.device.turnOn(window.id)
else:
    indigo.device.turnOff(window.id)

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Sun Jun 05, 2016 11:27 am
zama36 offline
Posts: 6
Joined: Mar 23, 2016
Location: Washington

Re: Need help w/ condition script or alternative suggestion

Thanks for the info. Unfortunately, it never goes into the if/else statement. Note I'm using the living room lights for testing. Also, I'm receiving the an error which I will add at the end.

Here's what it looks like when it runs

Code: Select all
house_temperature = float (indigo.devices[678652363].states['temperatureInput1'])
indigo.server.log("House is " + str(house_temperature))
outside_temperature = float(indigo.devices[126958715].states['temp'])
indigo.server.log("Outside is " + str(outside_temperature))
diff_temperature = house_temperature - outside_temperature
indigo.server.log("Difference is " + str(diff_temperature))
window_a = indigo.devices[1657238355].states['onOffState']
indigo.server.log("Device " + str(window_a))

if house_temperature > 70:
   if diff_temperature >= 4:
      if window_a:
         indigo.server.log("If statement reached")
         indigo.device.turnOn(1104590197)
else:
    indigo.server.log("else statement reached")
#   indigo.device.turnOff(1104590197)

indigo.server.log("After If/else")
indigo.device.turnOn(1104590197)


Jun 5, 2016, 10:23:22 AM
Script House is 76.0
Script Outside is 73.6
Script Difference is 2.4
Script Device True
Script After If/else
Z-Wave sent "Living Room Lights" on

If I uncomment indigo.device.turnOff(1104590197) in the else statement I receive the following error while compiling.
Jun 5, 2016, 10:24:54 AM
Script Error embedded script: unexpected indent
Script Error around line 17 - "indigo.device.turnOff(1104590197)"

Posted on
Sun Jun 05, 2016 11:30 am
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Need help w/ condition script or alternative suggestion

You can't have an empty statement after a colon, if you commented out the TurnOff then you are ending with a comment and that's going to break the code, swap the turnoff with the log so you end on a command.

I modified my post to include some sample code for you, bear in mind that copy and paste may be off so make sure you TAB in each indent just to make sure everything is evenly tabbed.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Sun Jun 05, 2016 11:32 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Need help w/ condition script or alternative suggestion

Python indenting has to be consistent: That is, it should either use tabs or use spaces but not both. I suspect that you have tabbed some entries to get indents and used spaces on others.

Ideally, a future version of Indigo (7?) would fix this by always converting tabs to spaces so that the code indenting is consistent. :)

Posted on
Sun Jun 05, 2016 11:36 am
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Need help w/ condition script or alternative suggestion

Try my code at the bottom of my post a couple posts back, in looking at your code you aren't getting the desired results because of your indenting:

Code: Select all
house_temperature = float (indigo.devices[678652363].states['temperatureInput1'])
indigo.server.log("House is " + str(house_temperature))
outside_temperature = float(indigo.devices[126958715].states['temp'])
indigo.server.log("Outside is " + str(outside_temperature))
diff_temperature = house_temperature - outside_temperature
indigo.server.log("Difference is " + str(diff_temperature))
window_a = indigo.devices[1657238355].states['onOffState']
indigo.server.log("Device " + str(window_a))

if house_temperature > 70:
   if diff_temperature >= 4:
      if window_a:
         indigo.server.log("If statement reached")
         indigo.device.turnOn(1104590197)
else: # <-------------------------------------------------- This else says if house is NOT over 70, should it be indented or rewritten so test for ALL THREE conditions instead?
    indigo.server.log("else statement reached")
#   indigo.device.turnOff(1104590197)

indigo.server.log("After If/else")
indigo.device.turnOn(1104590197)


Your code is saying "if the house is over 70 then.... else turn off" and ignores the rest of your conditions, my code above allows you to else all three conditions at once.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Sun Jun 05, 2016 11:38 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Need help w/ condition script or alternative suggestion

Also once the script works, you have to consider that the checks need to be made when any of the values you are checking in the script change. For example all conditions might be met other than Window B is closed. If Window B is opened then the AC should be turned off as all conditions are now met. So you need to trigger the script on House temp change, Outside temp change, Window A (opened or closed) and window B (opened or closed). :)

Posted on
Sun Jun 05, 2016 11:43 am
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Need help w/ condition script or alternative suggestion

Updated script based on auto's suggestion:

Code: Select all
thermostat = indigo.devices[678652363]
weather = indigo.devices[126958715]
windowa =  indigo.devices[1657238355]
windowb =  indigo.devices[WINDOW B'S ID]
tempdiff = weather.states['temp'] - thermostat.states["temperatureInput1"] # Both of these states are already floats, you shouldn't have to convert them

if thermostat.states["temperatureInput1"] > 70 and tempdiff >= 4:
     # This seems backwards to me, you are saying "if window on/off state is ON then turn it ON again", should this be reversed?
     if windowa.states["onOffState"]:
          indigo.device.turnOn(windowa.id)
     if windowb.states["onOffState"]:
          indigo.device.turnOn(windowb.id)
else:
     indigo.device.turnOff(windowa.id)
     indigo.device.turnOff(windowb.id)



Of course at this point I'm not sure if I'm totally clear on what should happen when :).

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Sun Jun 05, 2016 12:05 pm
zama36 offline
Posts: 6
Joined: Mar 23, 2016
Location: Washington

Re: Need help w/ condition script or alternative suggestion

:D :) THANK YOU!!! :) :D

With your suggestions I was able to get things working properly!!!

MUCH APPRECIATED!!!

This is what I came up with. Much cleaner than what I originally had.

Code: Select all
thermostat = indigo.devices[678652363]
weather = indigo.devices[126958715]
window =  indigo.devices[1657238355]
tempdiff = thermostat.states["temperatureInput1"] - weather.states['temp']

#indigo.server.log("Thermostat: "+str(thermostat.states["temperatureInput1"]))
#indigo.server.log("Weather: "+str(weather.states['temp']))
#indigo.server.log("Temp Diff: "+str(tempdiff))
#indigo.server.log("Window: "+str(window.states['onOffState']))

if thermostat.states["temperatureInput1"] > 70 and tempdiff >= 5 and window.states['onOffState']:
   indigo.device.turnOn(1104590197)
   indigo.server.log("House Fan On. House Temp: "+str(thermostat.states["temperatureInput1"])+" Weather: "+str(weather.states['temp']))
else:
   indigo.device.turnOff(1104590197)
   indigo.server.log("House Fan Off. House Temp: "+str(thermostat.states["temperatureInput1"])+" Weather: "+str(weather.states['temp']))


I'm using standard conditionals to ensure that the heat is off (Don't have AC.) and the script is only run from late spring to early fall.

Posted on
Sun Jun 05, 2016 1:24 pm
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Need help w/ condition script or alternative suggestion

Awesome, I'm glad you go things working :).

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest