more G-Labs products

Author Topic: Checking for cloudy conditions  (Read 3959 times)

April 25, 2014, 08:39:18 AM
Read 3959 times

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Hi there, I have written a simple javascript Trigger Code to check for cloudy conditions, basically it will look like this (code is not yet properly tested)

I would like to move the check for cloudy conditions to a function within a library somewhere, where can I best place the code for this?

Code: [Select]
// Put your trigger code logic here.
// Call 'hg.SetConditionTrue()' when you want
// the 'Code To Run' to be executed.
hg.SetConditionFalse();

// Switch on the Living Room lights
// Runs twice per day, at @Evening.GoldenHour.Start and @Evening.Sunset.Start
// At @Evening.GoldenHour.Start, check for cloudy conditions, if not cloudy (isClear = false) wait for Sunset

if (hg.Scheduler.IsScheduling("@Evening.GoldenHour.Start"))
{
  // Get Weather info
  var module = hg.Modules.WithName("Weather Underground").Get();
  // Get the icon name from IconUrl
  var iconName = module.Parameter("Conditions.IconUrl").Value.split("k/")[1];

  // Create an array of 'Clear' icon names
  var clearArray = ["clear.gif", "mostlysunny.gif", "partlycloudy.gif", "partlysunny.gif", "sunny.gif"];
 
  // Check to see if the iconName is found in the array
  var isClear  = clearArray.indexOf(iconName) > 0;

  if (isClear == false)
  {
    hg.SetConditionTrue();
  }
}
else if (hg.Scheduler.IsScheduling("@Evening.Sunset.Start"))
{
  hg.SetConditionTrue()
}
« Last Edit: April 25, 2014, 08:42:09 AM by mvdarend »

April 27, 2014, 04:17:05 PM
Reply #1

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
You can move it into the Weather Underground widget itself and make it add a new parameter, eg. "Conditions.IsCloudy".

Cheers,
g.

April 27, 2014, 08:18:32 PM
Reply #2

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Thanks for that, I didn't think of putting it there.

I broke the Weather Underground module when saving though... got the following error:
(https://twd94q.dm2303.livefilestore.com/y2pxNxr-TM4A9lZv75KNsnEu2x3z1rfme6Y3BDbUvmEnhj872eD5wPQhAs8yAh1ibWvVJigaa2UpExZQoU6qYHExtKbjPr1D-COxDjF3eMKbWM/WundergroundError.png?psid=1)

I can't seem to fix it now, anyone know what I need to do?

April 27, 2014, 08:53:18 PM
Reply #3

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Try updating the program again.
Are you running HG in Windows?

Cheers,
g.

April 27, 2014, 09:22:22 PM
Reply #4

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
No, I'm running it on the Raspberry Pi now.

What do you mean by 'try updating the program again'? Do you mean saving the code again, or updating HG?

I've tried Actions -> Update a few times. Removed my added code and tried it again, to no avail, I keep getting a 'Metadata file `/usr/local/bin/homegenie/Raspberry.IO.dll' does not contain valid metadata' error

As a test I went to Devices and Things -> Generic IP camera -> Edit -> Edit Source Code and then without changing anything I went to Actions -> Update and got the same error. Now it also has the Red disabled icon.

April 27, 2014, 09:34:57 PM
Reply #5

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Yes I meant Actions->Update from the code editor.
Try stopping HG and replacing the attacched DLLs (these are from the last version).
Do a sudo rm Raspberry.* before replacing the files.
(hg is located in /usr/local/bin/homegenie folder)

Also what version of HG are you running?

g.
« Last Edit: April 27, 2014, 09:37:31 PM by Gene »

April 27, 2014, 09:46:40 PM
Reply #6

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431

That fixed it, thanks  :)

- I'm running r371, in case you still need the info.

April 28, 2014, 01:08:17 AM
Reply #7

Jan

  • **
  • Information
  • Jr. Member
  • Posts: 34
Hi mvdarend,

If I may intrude into your conversation: I just uploaded a new version of my little widget which uses openweathermap.org as a source for the data.

You can find it attached here: http://www.homegenie.it/forum/index.php?topic=142.msg974#new

The benefit is that you don't need an API key and also it provides cloudiness in % as one parameter.

This depends a bit on the local weather station you choose. But I found it almost for all stations to be provided.

So, just in case you are interested, you could install this widget and then use the parameter

Code: [Select]
jkUtils.OpenWeatherMap.Clouds.All
which holds a value between 0 (no clouds) and 100 (fully clouded) instead of parsing the weatherunderground icon.

Best,
jan

April 28, 2014, 06:58:02 AM
Reply #8

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Thanks Jan, that's exactly what I need :) Unfortunately, the nearest weather station is about 80 km away, so the cloud cover measurement won't be very accurate.

The WU description is also not as accurate as I would like, I think I'm better off with a light sensor.

April 28, 2014, 10:49:47 AM
Reply #9

Jan

  • **
  • Information
  • Jr. Member
  • Posts: 34
Hey mvdarend,

Have you checked your location already?

You can do this on the offical site als well in case you don't want to install the widget.

http://openweathermap.org/

I'm asking because they have more than 40.000 stations in their network. So it's not just the official weather stations by puclic services but also private people who upload the data of their own stations.

So, chances should be higher to have a match.

But of course, nothing beats a local sensor on you own turf  ;)

Best,
jan

April 28, 2014, 10:53:55 AM
Reply #10

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Thanks for the tip Jan, I searched (properly this time) on the website, and found the tab 'Stations' this looks pretty good :)

April 28, 2014, 09:10:24 PM
Reply #11

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
I've installed the OpenWeatherData plugin and modified the code, tomorrow I'll know if it works, below is the trigger code for anyone that's interested. (not yet properly tested).

By the way, your OpenWeatherData plugin looks great, I really like it. Weather underground is already disabled :))

Code: [Select]
// Switch on the Living Room lights
// Check at @Evening.GoldenHour.Start and @Evening.Sunset.Start
// At @Evening.GoldenHour.Start, check for cloudy conditions,
// if cloudy (jkUtils.OpenWeatherMap.Clouds.All >= 60) then trigger, otherwise wait for sunset

var goldenHourStart = hg.Scheduler.WithName("SunCalc.Evening.GoldenHour.Start");
var sunsetStart = hg.Scheduler.WithName("SunCalc.Evening.Sunset.Start");

if (goldenHourStart.IsScheduling())
{
  // Get Weather info
  var owd = hg.Modules.WithName("jkUtils - OpenWeatherData").Get();
  var cloudiness = parseInt(owd.Parameter("jkUtils.OpenWeatherMap.Clouds.All").Value);
  if (cloudiness >= 60)
    {
       hg.SetConditionTrue();
    }
}
else if (sunsetStart.IsScheduling())
{
  hg.SetConditionTrue()
}
« Last Edit: May 02, 2014, 08:47:03 PM by mvdarend »

May 02, 2014, 08:47:31 PM
Reply #12

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Thanks Jan, it works perfectly!

May 02, 2014, 11:37:03 PM
Reply #13

Jan

  • **
  • Information
  • Jr. Member
  • Posts: 34
Glad to hear!

Really nice approach to include the cloudiness into calculating the dusk time. I hadn't thought of this before, but it makes so much sense.  :-)

So, thanks for sharing.
Best,
jan