HomeGenie Forum

Automation Program Plugins and Wizard Scripting => APP Contributions => Topic started by: Jan on May 09, 2014, 12:12:04 AM

Title: Solar Altitude (replaces: SunCalc)
Post by: Jan on May 09, 2014, 12:12:04 AM
Hi all,

after some learning with reflow/scalability from coding the OpenWeatherMap-Widget, I went back to rework the SunCalc Widget accordingly.

So here you have it: the new Solar Altitude Widget.

I gave it a new name since:


Most important new features include:


Let me know, what you think.

Best,
jan

Title: Re: Solar Altitude (replaces: SunCalc)
Post by: mvdarend on May 09, 2014, 08:21:32 AM
Very nice, I've just installed it and it looks great, now off to install the new Weather widget.  :)
Title: Re: Solar Altitude (replaces: SunCalc)
Post by: dutchie on May 17, 2014, 04:48:13 PM
Where can I find a list with the colorcodes (Hue value)?

*edit
Never mind
Didn't notice there is a slider in it  :-[
Title: Re: Solar Altitude (replaces: SunCalc)
Post by: Jan on May 31, 2014, 12:03:10 AM
Hi Gene,

I recently noticed the NaN problem appearing again (see attached screenshot). So far I could trace it back to the HG-script computing the timings and not the javascript included in the json. Since I haven't really changed anything to this part over the last releases, my question is, whether this might be linked to the javascript engine of homegenie?

Were there any changes to this part over the last updates of HG?
Or do I have to try looking some place else ? ;)

Cheers,
jan
Title: Re: Solar Altitude (replaces: SunCalc)
Post by: badfinger on May 31, 2014, 11:29:38 AM
Hi Jan,
I've got the same issue. The NaN error dissapears when i erase the longitude and latidude values. After that all works fine, but with "Timbuktu" or any else timezone ;-))

Grue

Alex

Title: Re: Solar Altitude (replaces: SunCalc)
Post by: Gene on May 31, 2014, 03:20:08 PM
Hi Jan,

this seem to be a number formatting/parsing error. You should check how the number is passed to the function that parses it.
Most commonly the decimal separator can cause such problems when using different language settings on the server (internationalization settings should be kept to en-US on raspberry to prevent such issues, still you can change the timezone if needed).

Also some other user reported me another problem with the widget. This other problem may be related to a wrong file encoding.
If someone else is experiencing same problem (see attacched picture) and know how to use the browser's developer tools, please verify the following:

1) open the widget's json and html file directly in your browser and using the Network tab in the develpers tools see what econding is sent from server (see attacched pic)

http://your_raspi_address/hg/html/pages/control/widgets/jkUtils/SolarAltitude/SolarAltitude.html (http://your_raspi_address/hg/html/pages/control/widgets/jkUtils/SolarAltitude/SolarAltitude.html)
http://your_raspi_address/hg/html/pages/control/widgets/jkUtils/SolarAltitude/SolarAltitude.json (http://your_raspi_address/hg/html/pages/control/widgets/jkUtils/SolarAltitude/SolarAltitude.json)

2) look both files source if text is containing wrong encoded characters.

Just to let you know, I am not experiencing none of these issues. SolarAltitude is working well for me.

g.
Title: Re: Solar Altitude (replaces: SunCalc)
Post by: Gene on June 01, 2014, 08:30:07 PM
Hi Jan,

finally I updated the Android app so that now Solar Altitude and OpenWeather works.
Currently I only wrote the adapters for loading data into the standard weather and earth tools widgets.
Didn't write a specific widget for now.

I have a couple of issues though:

- the OpenWeather icons are not transparent and have white background
- there's no easy way to get the url of the Solar Altitude widget (I put a fixed icon for now)

So I would suggest to put the code for the SolarAltitude icon in the app rather than in the widget.
I will wait some more before publishing new hg and new android app, just in case you can help with this in the meantime.

Cheers,
g.
Title: Re: Solar Altitude (replaces: SunCalc)
Post by: Gene on June 02, 2014, 02:56:14 PM
Hi Jan,

I made the images transparent for the OpenWeatherMap widget (inside htmlpagescontrolwidgetsjkUtilsOpenWeatherMapimagesicons).

So if you have a chance, can you provide any tip about how to set the SolarAltitude icon image url to be used in the program code instead of the widget json?
The widget json code it's not very familiar to me =) so you may go faster in finding a solution for it.

Thanks!
g.
Title: Re: Solar Altitude (replaces: SunCalc)
Post by: Jan on June 02, 2014, 11:29:42 PM
Hi Gene,

Thanks a lot for the effort with bringing the modules to the app as well!

I'm totally packed this week until Friday - but I?ll most definitely get back to you then with some details  on how to select the matching labels and icons.

I can also provide you with another export of the icons with transparent background in whatever size you need them since I have them here as vector graphics. Let me know whether you're fine with your converted versions or if you want me to export them again - It's no biggie.

Sorry for being so brief, but this week is really killing me.
Talk to you on Friday...

Best,
jan

Title: Re: Solar Altitude (replaces: SunCalc)
Post by: Gene on June 02, 2014, 11:52:49 PM
Hi Jan,

transparent images are ok, no need to export. I just published r374, so no hurry, we'll fix this for the next release.
Have a good week and talk to you soon then =)

Cheers,
g.
Title: Re: Solar Altitude (replaces: SunCalc)
Post by: Jan on June 12, 2014, 09:40:43 PM
Hi Gene,

sorry, long time and no word from my side.
I'll try to make up for this by posting the logic of how I construct the label for the current sunphase.
I extracted the relevant code and generalized it a bit and added comments to explain what is happening.
Let me know if this is self-explaining enough to adapt it for the app or if I need to elaborate a bit more. Here we go:

Code: [Select]

// used variable

var DataTimers = { // this object contains all calculated timestamps for phases start and end in a chronological order
label: // Timestamp label.
// The variable "label" simply contains the variable name from the widget settings that holds the phase time.
// e.g. "jkUtils.SolarAltitude.Evening.Nautical.Start" => "20:52"
//
  // It is used to resolve the localized value of the span with the matching attribute "data-local-id"=label from within
// the hidden settings section of the html template, e.g.
// "jkUtils.SolarAltitude.Morning.Nautical.Start" references the span containing "Nautical Dawn" as text.
time: // unix-timestamp
timeStr: // time as string in 24h format, e.g. "02:01"
timePrefix:     // 'at', 'till' or 'since'
icon: // icon file name, e.g. "Evening.Nautical.Start.png"
                                // the icon-filename itself is always the variable name without the plugin-name-prefix
                                // e.g. jkUtils.SolarAltitude.Evening.Nautical.Start => Evening.Nautical.Start.png
                                // those icons are located in the images/status sub-folder
};

// for the timePrefix part of the object:
// this is hard coded for the relevant/used phases. I currently use the following timers for display:

[ 'Morning.Night.End', 'till' ],
[ 'Morning.Astronomical.Start', 'since' ],
[ 'Morning.Nautical.Start', 'since' ],
[ 'Morning.Civil.Start', 'since' ],
[ 'Morning.Sunrise.Start', 'since' ],
[ 'Morning.Sunrise.End', 'at' ],
[ 'Morning.GoldenHour.Start', 'since' ],
[ 'Morning.GoldenHour.End', 'since' ],
[ 'Day.Noon', 'at' ],
[ 'Evening.GoldenHour.Start', 'till' ],
[ 'Evening.GoldenHour.End', 'till' ],
[ 'Evening.Sunset.Start', 'at' ],
[ 'Evening.Sunset.End', 'till' ],
[ 'Evening.Civil.End', 'till' ],
[ 'Evening.Nautical.End', 'till' ],
[ 'Evening.Astronomical.End', 'till' ],
[ 'Evening.Night.Start', 'since' ],
[ 'Night.Nadir', 'at' ]

//-------------------------------------------------------------------------
// -- START: Code for composing the phase display text

//-- Setup Variables
//
var now = new Date(), // get current timestamp
currentTime = 0,
description, offset, closestOffset;

//-- Find Closest Timestamp To Current Time
//
closestOffset = Infinity; //initially set offset to max value (infinity)
for( var i = 0; i < DataTimers.length; ++i ) { //calculate which given timestamp is closest to current timestamp
var loopTimer = DataTimers[i].time;
offset = Math.abs( +loopTimer - +now );
if ( offset < closestOffset ) {
closestOffset = offset;
currentTime = i;
}
}

//-- Exceptions: pick another timestamp under certain conditions:
//
//
//-- Jump To Next Timestamp When Current Time Is Past 'till' Or 'at'
if( now > DataTimers[currentTime].time &&
( DataTimers[currentTime].timePrefix == 'till' || DataTimers[currentTime].timePrefix == 'at' ) ) currentTime++;

//-- next Exception:
//
//
//-- Jump Back To Previous Timestamp When Time Before 'since'
if( now < this.DataTimers[currentTime].time &&
this.DataTimers[currentTime].timePrefix == 'since' ) currentTime--;


//-- Construct Current Phase Display Text
//
description += '<span>' + DataTimers[currentTime].timePrefix + '</span><span>' + DataTimers[currentTime].timeStr + '</span><br/>';

// -- END: Code for composing the phase display text
Title: Re: Solar Altitude (replaces: SunCalc)
Post by: Jan on June 12, 2014, 10:03:06 PM
Ups, I hit send to early. But it might make sense to put this in a 2nd post anyways.

So, concerning the earlier mentioned problem with the NaN Display:
I still experience this problem, unfortunately.

Quote
this seem to be a number formatting/parsing error. You should check how the number is passed to the function that parses it.
Most commonly the decimal separator can cause such problems when using different language settings on the server (internationalization settings should be kept to en-US on raspberry to prevent such issues, still you can change the timezone if needed).


Quote
1) open the widget's json and html file directly in your browser and using the Network tab in the develpers tools see what econding is sent from server

Encoding is ok. I also checked and changed the language settings. Still, the json already has "NaN" literally as a value. So it does not seem to be a parsing error in the javascript interpretation of the json.

Interestingly, the function in the HG-JS-Part is always the same. For some values it works, while for others it doesn't. Strange thing is that it is always the same calculation. The only difference is the angle of the sun above the horizon as a basis for the operation. But this always is a full integer / whole number.

One other interesting observation is that that I always get a compiler error for some releases now when I try to save the javascript in HG automation editor. This even happens when I make no changes at all. The script works as fine. But as soon as I hit save (and ever since that moment) I can't re-save the script (no matter what I do/don't change) without a compiler error.

Could you just verify, if you can re-save the script with no modifications and not getting a compile error?

I'm a bit clueless here. But this observation originally led to my earlier assumption/question on whether this might be linked to recent changes in the js-interpreter of .net.

Thanks for your input and all the best,
jan
Title: Re: Solar Altitude (replaces: SunCalc)
Post by: Jan on June 15, 2014, 03:02:53 PM
Hi Gene,

just to let you know: I might be on to something to solve the NaN problem. (might have to do with how acos-calculation are handled by javascript).

So not need to invest any time from your side in this for the moment.

I'll get back to you when I know more

Cheers,
jan
Title: Re: Solar Altitude (replaces: SunCalc)
Post by: Jan on June 21, 2014, 12:02:48 AM
Ok, I think I found it.

Indeed the acos-function of Javascript was causing trouble.

To fix this, replace the getHourAngle function in line 151 of the widget script with:

Code: [Select]
function getHourAngle(h, phi, d) {
    var first = sin(h) - sin(phi) * sin(d),
        second = cos(phi) * cos(d),
        third = first / second;
    if ( third < -1 || third > 1 ) {
        third = Math.round(third);
    } 
  return acos(third);
}

or delete and reimport the widget using the fixed file attached (please note, that PID will change to value above 1000 since it then will be treated as a custom script)

Please let me know if this fixes your problem as well or if I have to continue investigating  ;)

j.
Title: Re: Solar Altitude (replaces: SunCalc)
Post by: mvdarend on June 21, 2014, 07:19:24 AM
Yep, that fixed it for me. Thanks!
Title: Re: Solar Altitude (replaces: SunCalc)
Post by: bkenobi on November 02, 2014, 05:08:23 PM
With daylight savings time ending last night, I found that this app needs a modification.  I have a light that turns on at dusk and off at dawn.  This light turned off at 7:57am yesterday.  Since daylight savings time moved sunrise an hour earlier today, the lights should have turned off at 6:47am today however they waited until 7:57am.  I don't know if Solar Altitude knows about daylight savings time or if perhaps it runs at a given time once a day and doesn't update again.  Either way, at least on the day of daylight savings time switch, sunrise/sunset values are wrong.

Also, since I'm posting anyway, when displaying the HG web page on my Android in vertical mode, the Solar Altitude display is pretty ugly due to the way things are split up.  If I rotate the device to horizontal, things are displayed correctly.  I don't know if there's much that can be done about this since the device is pretty narrow and HG doesn't let the display scroll horizontally.

Other than that, everything else works beautifully and has been for months!  The display issue isn't a huge issue since I usually use the HG Android App.  The daylight savings issue is possibly only an issue 2 days a year.
Title: Re: Solar Altitude (replaces: SunCalc)
Post by: mvdarend on November 02, 2014, 05:25:53 PM
Daylight savings ended for me last week but I had no problems with wrong times, everything went well as far as I remember. (I may have rebooted the BananaPi that day though, I can't remember)

Have you checked the Solar Altitude times in the Scheduler, are they correct?
Title: Re: Solar Altitude (replaces: SunCalc)
Post by: bkenobi on November 02, 2014, 05:42:52 PM
Yes I checked and the times are wrong.  HG correctly turned the lights off at the time solar altitude indicated, but solar altitude was wrong by the 1 hour offset.