more G-Labs products

Author Topic: Programs keep randomly disabling themselves  (Read 2912 times)

November 24, 2015, 02:02:34 PM
Read 2912 times

jpscuba

  • **
  • Information
  • Jr. Member
  • Posts: 25
I have several programs to control my lights.  These programs run fine for a few days and then it seems that one program will become disabled.  I have separate programs for turning on lights and turning off lights.  Here is a sample of a lamp turn off program in python.  Do I have it wrong or is something wrong with my system? 

Program Code:
    hg.Modules.WithName('FRLampModule').Off()

Startup Code:
    import time

    if hg.Scheduler.IsScheduling("0 23 * * *"):
      hg.SetConditionTrue()
    else:
      hg.SetConditionFalse()
 
    time.sleep(20)

If I re-enable the disabled program it will run again for days or weeks.  I am running version 1.1 beta r498.
Any help is appreciated.

November 24, 2015, 04:50:52 PM
Reply #1

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I don't have any help for the program being disabled unfortunately.  I have seen where one APP is enabled when I back up my setup and yet it is disabled on restore (jkutils solar altitude).

On a different note, you probably don't need this script in the first place.  Unless you are doing something more complex than turning a light on/off at a given time, you can actually just use the scheduled on/off code that's built in.  I've been using this script since it was incorporated and, outside of some strange setup fluke in my system at one time, it's always worked correctly.

November 25, 2015, 03:22:23 AM
Reply #2

jpscuba

  • **
  • Information
  • Jr. Member
  • Posts: 25
I would use the scheduled on/off but it doesn't use sunrise and sunset from SolarAltitude so I had to write some scripts.  All was working find until I updated the homegenie version to beta R498.  Should I try to update to a later version?  HG is not finding the latest version on it's own.  Is there really a problem with my program?  Is there a way to have the program enable itself at startup?  I cannot seem to find these answers in the manual or the forum.  Can anyone point me to further info that will be of use to me?

November 25, 2015, 04:12:10 AM
Reply #3

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
Scheduled events work with anything that has a defined schedule or a cron formatted string.  If you have one of the weather APP's with sunrise/sunset enabled then you do get those scheduled events updated daily.  I use jkUtils solar altitude, but I believe Weather Underground and possibly jkUtils OpenWeatherMap all have sunrise/sunset.

November 25, 2015, 01:55:45 PM
Reply #4

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
I would use the scheduled on/off but it doesn't use sunrise and sunset from SolarAltitude so I had to write some scripts.

It took me a while to get the syntax right for SolarAltitude too, but the following format works:
Code: [Select]
@SolarAltitude.Evening.GoldenHour.Start
I've never seen a Program disable itself though. Can you tell me what the following two lines achieve? I've never seen a Pause statement in the Trigger startup code before.
Code: [Select]
import time
time.sleep(20)
« Last Edit: November 25, 2015, 04:11:34 PM by mvdarend »

November 25, 2015, 04:07:31 PM
Reply #5

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Startup code
Code: [Select]
if hg.Scheduler.IsScheduling("0 23 * * *"):
      hg.Program.Run()

Program code
Code: [Select]
hg.Modules.WithName('FRLampModule').Off()


November 25, 2015, 04:42:38 PM
Reply #6

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
Keep in mind that the Scheduled On/Off APP is just a C# program.  You can always modify it if you want to get the capabilities you need.

November 26, 2015, 03:07:54 PM
Reply #7

jpscuba

  • **
  • Information
  • Jr. Member
  • Posts: 25
Thanks for all the replies.  I was using the sleep in my programs because the programs were running constantly and using most of my processor on a Rasp Pi.  With the sleep it cut the CPU usage down to 1-2%.  I have upgraded to a Pi2 and have been reusing the programs I wrote about 2 years ago when some of the fetures were still buggy.  I also could not get the syntax correct when I tried it about a year ago after SolarAltitude was incorporated, thanks mvdarend for the correct syntax.

Thanks Gene for the corrected startup and program code.  I have updated my programs to consolidate them into a light control for each light instead of a program for on and a separate program for off as follows.  Yes I could use the built in scheduler but I am also trying to learn programming of the system so I can add more complex functinality at a later time.  My current programs are as follows:

Startup:
hg.Program.Run()

Program:
import time

if hg.Scheduler.WithName('SolarAltitude.Evening.Sunset.Start').IsScheduling():
    hg.Modules.WithName('BRLampModule').On()
elif hg.Scheduler.IsScheduling("00 23 * * *"):
  hg.Modules.WithName('BRLampModule').Off()
time.sleep(20)

I assume that this startup causes the program to run constantly but I am still using only 8% CPU with all  programs running.  Would it be better to have a separate programe for on and off and have  the startup check the conditional and then call program.run() or is what I have about the same CPU wise?

    Joe

November 26, 2015, 03:18:26 PM
Reply #8

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
so I wonder why don't you use the scheduled on/off feature?


November 26, 2015, 03:32:44 PM
Reply #9

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
by the way, the cpu usage problem is due to continuos program initialization (the startup code), so a better solution for your code would be changing it like this:

Program
Code: [Select]
import time

while (hg.Program.IsEnabled)
    if hg.Scheduler.WithName('SolarAltitude.Evening.Sunset.Start').IsScheduling():
       hg.Modules.WithName('BRLampModule').On()
    elif hg.Scheduler.IsScheduling("00 23 * * *"):
       hg.Modules.WithName('BRLampModule').Off()
    time.sleep(20)

this way the program will be only initialized one time, and it will run in the background.

g.

November 26, 2015, 03:37:37 PM
Reply #10

jpscuba

  • **
  • Information
  • Jr. Member
  • Posts: 25
Thanks Gene,
     I'll try the scheduler for the simple light control and also change the programs that will get more complex at a later time.  I plan on adding some scenes to the lamps later and wanted to learn the correct programming now.


November 26, 2015, 03:59:29 PM
Reply #11

jpscuba

  • **
  • Information
  • Jr. Member
  • Posts: 25
I just tried the scheduler on one of my outside light modules and it did not work.  I now remember that this is why I didn't use it in the past.  I did update the programs as you suggested with the while loop and it has reduced my CPU usage down to 1/2% which is great.  I also wrote a program for the same light module which worked fine.  I have attached the scheduler screenshot to show what I had programmed.


November 26, 2015, 04:18:33 PM
Reply #12

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
You need to activate the scheduler or it won;t work.

November 26, 2015, 04:31:24 PM
Reply #13

jpscuba

  • **
  • Information
  • Jr. Member
  • Posts: 25
I just retried it as such and still not working.  Nothing in the log files either.


November 26, 2015, 05:28:26 PM
Reply #14

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
hi jpscuba,

ensure the timezone is correctly set on your hg server. Also verify that the Scheduled On/Off program is enabled and running (it should since you see options in the module popup).

g.