[ANSWERED]: Python if/then/else syntax issue?

Posted on
Thu Dec 04, 2014 9:10 am
Al_M offline
Posts: 19
Joined: Nov 17, 2014

[ANSWERED]: Python if/then/else syntax issue?

This is my first post here. Been running my indigo based automation system for several months. Now I'm ready to start some scripting for some advanced tasks.

So I am trying to automate my humidifier solenoid control with a z-wave relay module. The solenoid will be energized/de-energized based on a comparison of the average indoor relative humidity (RH_Avg) and a setpoint generated in another script (RH_SP). If the average indoor relative humidity is less than the desired setpoint, then the demand to turn on the solenoid (Humid_Demand) is set to True, otherwise it is False. This would seem a relatively simple if/then/else statement, but i think I may have a syntax issue. I am trying to run it as an embedded script that runs periodically (on a schedule) or I may consider setting it up based on a trigger at some point (for example when the furnace kicks on). Right now I am just trying to get it to evaluate correctly, but testing shows that it does not. Each time the script runs, all it does is flip the state of Humid_Demand from True to False or vice-versa (toggles back and forth). This doesn't make sense since I know the expression can only evaluate to one state and the variables (RH_Avg and RH_SP) are not changing. Obviously this won't work for controlling the solenoid valve.

Any ideas or suggestions as to what I am doing wrong would be appreciated.

Code: Select all
RH_Avg = indigo.variables[83180808] # "Humid_Avg_Indoor"
RH_SP = indigo.variables[370832338] # "Humidity_SP"

if RH_Avg < RH_SP:
    indigo.variable.updateValue(indigo.variables[91486408], unicode(True)) # "Humid_Demand" set to True
else:
    indigo.variable.updateValue(indigo.variables[91486408], unicode(False)) # "Humid_Demand" reset to False

Posted on
Thu Dec 04, 2014 9:49 am
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Python if/then/else syntax issue?

Add ".value" to the end of your first two lines:

Code: Select all
RH_Avg = indigo.variables[83180808].value    # "Humid_Avg_Indoor"
RH_SP = indigo.variables[370832338].value    # "Humidity_SP

And you probably also want to cast them to floats so the compare works (they are strings by default):

Code: Select all
RH_Avg = float(indigo.variables[83180808].value)    # "Humid_Avg_Indoor"
RH_SP = float(indigo.variables[370832338].value)    # "Humidity_SP

Also for the updateValue lines you can just pass in the variable IDs (no need to do a full variable elem lookup):

Code: Select all
if RH_Avg < RH_SP:
    indigo.variable.updateValue(91486408, unicode(True)) # "Humid_Demand" set to True
else:
    indigo.variable.updateValue(91486408, unicode(False)) # "Humid_Demand" reset to False

Image

Posted on
Thu Dec 04, 2014 10:06 am
Al_M offline
Posts: 19
Joined: Nov 17, 2014

Re: [ANSWERED]: Python if/then/else syntax issue?

Wow! Fast response.

Thank you. Worked like a charm.

Posted on
Thu Dec 04, 2014 3:19 pm
DaveL17 offline
User avatar
Posts: 6756
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: [ANSWERED]: Python if/then/else syntax issue?

Just to add a (arguably obvious) thought, you'd *only* want to demand humidity when the HVAC fan is on--with or without heat to suit your taste.

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

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 6 guests

cron