more G-Labs products

Author Topic: Temperature Controlled Lights - Need a little help for a newbie  (Read 1570 times)

December 15, 2015, 10:37:05 AM
Read 1570 times

lancebooth

  • *
  • Information
  • Newbie
  • Posts: 6
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:

Code: [Select]
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:

Code: [Select]
// 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"
« Last Edit: December 15, 2015, 10:21:15 PM by lancebooth »

December 15, 2015, 02:23:52 PM
Reply #1

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
The easiest way is to use InGroup and OfDeviceType. You were on the right track, only the implementation needed to be slightly defferent.

Code: [Select]
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();
   }
}
« Last Edit: December 15, 2015, 02:29:52 PM by mvdarend »

December 15, 2015, 02:55:52 PM
Reply #2

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
Wonder its worth having a code snippets section in the app contributions bit on github??

December 15, 2015, 08:05:30 PM
Reply #3

lancebooth

  • *
  • Information
  • Newbie
  • Posts: 6
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
« Last Edit: December 15, 2015, 10:25:05 PM by lancebooth »

December 16, 2015, 09:14:37 AM
Reply #4

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Quote
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)

Code: [Select]
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
« Last Edit: December 16, 2015, 09:17:12 AM by mvdarend »

December 17, 2015, 06:52:19 AM
Reply #5

lancebooth

  • *
  • Information
  • Newbie
  • Posts: 6
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