HomeGenie Forum
General Category => General Discussion => Topic started by: lancebooth on December 15, 2015, 10:37:05 AM
-
I would like to control the lights around my house according to the temperature. I live in the south of the US and when it gets close to freezing I would like to turn on Christmas lights to ward off the somewhat colder temperatures...
I wrote a new program (borrowing heavily, well exclusively, from the HG application) and, I can apply it to a specific module or modules that apply by name, I cannot figure out how to apply it to all of the modules in a group, with out their names...
PROGRAM CODE:
int onTemp = int.Parse (Program.Option("onTemp").Value);
int offTemp = int.Parse (Program.Option("offTemp").Value);
string onCondition = (Program.Option ("onCondition").Value);
// Modules.InGroup(PROGRAM_OPTIONS_STRING).OfDeviceType("Light,Dimmer,Switch").Each (mod => {
int temperature_f = int.Parse (Program.WithName("Weather Underground").Parameter("Conditions.TemperatureF").Value);
var light = Modules.WithName("Outside Front");
if (onCondition == "TRUE") {
if (temperature_f < onTemp) {
light.On();
} else if (temperature_f > offTemp) {
light.Off();
}
} else {
if (temperature_f > onTemp) {
light.On();
} else if (temperature_f < offTemp) {
light.Off();
}
}
light = Modules.WithName("Citrus Tree Lights");
if (onCondition == "TRUE") {
if (temperature_f < onTemp) {
light.On();
} else if (temperature_f > offTemp) {
light.Off();
}
} else {
if (temperature_f > onTemp) {
light.On();
} else if (temperature_f < offTemp) {
light.Off();
}
}
//});
TRIGGER CODE:
// this Setup delegate will be executed once, when program become active
Program.Setup(()=>{
// set input fields parameters
// <field_name>, <default_value>, <description>
// Program.AddOption(<field>, <defaultValue>, <description>, <type>)
Program.AddOption("offTemp", "42", "The temperature to turn off the lights.", "int");
Program.AddOption("onTemp", "38", "Temperature to turn on the lights", "int");
Program.AddOption("onCondition", "TRUE", "For instance: to keep lights from flickering TRUE if lights come 'ON' < 38 and 'OFF' > 42", "bool");
});
//
return true; // execute "Code To Run"
-
The easiest way is to use InGroup (http://genielabs.github.io/HomeGenie/api/ape/a00006.html#ac0054513e11de5db226eea17c45badc7) and OfDeviceType (http://genielabs.github.io/HomeGenie/api/ape/a00006.html#a2a6978a61bb3814a3ebec51343078388). You were on the right track, only the implementation needed to be slightly defferent.
var lights = Modules.InGroup("Outside Lights").OfDeviceType("Light,Dimmer,Switch");
if (onCondition == "TRUE") {
if (temperature_f < onTemp) {
lights.On();
} else if (temperature_f > offTemp) {
lights.Off();
}
} else {
if (temperature_f > onTemp) {
lights.On();
} else if (temperature_f < offTemp) {
lights.Off();
}
}
-
Wonder its worth having a code snippets section in the app contributions bit on github??
-
Works like a charm!
An app contributions section on github probably would have helped me... I went through the documentation on the HomeGenie site, and was having trouble finding sample code to help me, which is why I posted here...
I do have one further question... Is there a way to dynamically get the group name instead of hard-coding it? I would like to be able to add the module to a group and have it control the lights without having to use a specific group name.
Thankyou very much for your help.
Lance
-
I do have one further question... Is there a way to dynamically get the group name instead of hard-coding it? I would like to be able to add the module to a group and have it control the lights without having to use a specific group name.
If you want to switch all lights on and off manually you can use the 'All lights on' and 'All lights off' programs (Programs -> Scenes).
The code is quite simple, maybe something like the following will work to automate it: (This looks a little bit like your original code)
var lights = Modules.InGroup(PROGRAM_OPTIONS_STRING).OfDeviceType("Light,Dimmer,Switch");
if (onCondition == "TRUE") {
if (temperature_f < onTemp) {
lights.On();
} else if (temperature_f > offTemp) {
lights.Off();
}
} else {
if (temperature_f > onTemp) {
lights.On();
} else if (temperature_f < offTemp) {
lights.Off();
}
}
And then add the program to the correct group via Configure -> Groups
Edit: You might have seen this already, but there are som good examples here:
http://genielabs.github.io/HomeGenie/programs.html (http://genielabs.github.io/HomeGenie/programs.html)
-
Thank you for responding...
I actually saw the PROGRAM_OPTIONS_STRING but being an old C (not C++) programmer, it looked like a constant to me... I didn't realize it was set dynamically.
I will test it in the morning, since (unusual for this time of year) it is already freezing here and I fear my fruit trees will suffer (and thus the citrus fruit) if I have to test it too much. :-)
Lance