more G-Labs products

Author Topic: Global functions  (Read 1054 times)

December 14, 2015, 11:37:57 AM
Read 1054 times

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Is there some way to have a global function in HG?

What I'm trying to achieve is to get the correct light values depending on the time of day, currently I'm doing this as follows:
Code: [Select]
// standard theme is Relax
var command = ".1997,.8346,.8";

var solarAltitude = Modules.WithName("jkUtils - Solar Altitude").Get();
// Get Evening GoldenHour start
DateTime EveningGoldenHourStart = DateTime.Parse(solarAltitude.Parameter("jkUtils.SolarAltitude.Evening.GoldenHour.Start").Value);
DateTime EveningSunsetEnd = DateTime.Parse(solarAltitude.Parameter("jkUtils.SolarAltitude.Evening.Sunset.End").Value);

if (DateTime.Now.Hour == 6)
{
  // Energize theme
  command = "0.52635,.9133,.8";
}
else if (DateTime.Now.Hour >= 7 && DateTime.Now < EveningGoldenHourStart)
{
  // Concentrate theme
  command = "0.51649,.1732,.8";
}
else if (DateTime.Now >= EveningGoldenHourStart && DateTime.Now < EveningSunsetEnd)
{
  // Reading theme
  command = ".23393,.4763,.8";
}

Modules.WithName("Dining table light").Command("Control.ColorHsb").Set(command);

The problem is that I'm using this code for every single light (they're linked to seperate Z-Wave wall switches), what I'd prefer is to move the 'Get correct theme' code to a central location and just use something like this:

Code: [Select]
var command = HomeGenie.GetCurrentLightTheme();
Modules.WithName("Dining table light").Command("Control.ColorHsb").Set(command);

December 14, 2015, 07:13:48 PM
Reply #1

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
I could save the correct values in a global parameter, which I can get when needed.
Code: [Select]
Settings.Parameter("CurrentHSB").Value = ".1997,.8346,.8";
I can then set the correct values at set times of the day (Sunset, Sunrise, GoldenHour etc.) but then I still have one question. How can you make a program run at HomeGenie startup?
« Last Edit: December 14, 2015, 07:27:09 PM by mvdarend »

December 14, 2015, 07:38:04 PM
Reply #2

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
The right way would be having a webservice api method to call.
Then you can probably query the value using something like this:

Code: [Select]
var currentHsb = Modules.WithName("My Program").Command("Variables.CurrentHsb").GetValue();

If you want your value to be persistent (eg. exists even after a restart, use Program.Store to store it).

http://genielabs.github.io/HomeGenie/api/ape/a00009.html#a111c3e247c9a22cf44e032bfb568f876
http://genielabs.github.io/HomeGenie/api/ape/a00014.html

To run a program when HG starts use this:

http://genielabs.github.io/HomeGenie/api/ape/a00001.html#afcf0d379d8dd2da00c2adf1c3c9996f3

Cheers,
g.
« Last Edit: December 14, 2015, 07:44:18 PM by Gene »

December 14, 2015, 07:54:18 PM
Reply #3

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
That's great, thanks Gene.