[SOLVED] How to create a Timer to switch off a light?


(Pretheep K P) #1

I have create a new rules file called myhome.rules and written the following code in that file. But its not working. Any idea what can be the reason? The name of the Thing is SmallAquariumLight. Its a Switch. Installed on RPi3. I restarted the Rpi3 after writing the rule

rule “Switch on”
when
Time cron “51 0 22 ? * * *”
then
sendCommand(SmallAquariumLight,OFF)
end


(lipp_markus) #2

First of all, please use code fences:


You are not saying whether you see any errors in your log file:

Do you use VSC?


Does it show you any errors?

Please check that you are using straight " (in your post they were the curvy ones”, but that may be a copy paste problem.

What is very helpful in troubleshooting is adding a logInfo statement immediately after the then line of your rule, that will log when the rule triggered. Doing so will allow you to determine whether the rule triggered at all:

rule "switch on"
when 
     Time cron "51 0 22 ? * * *"
then
     logInfo("Switch on","My switch on rule triggered")
     SmallAquariumLight.sendCommand(OFF)
end

Please note that the syntax <item_name>.sendCommand(command) is much preferred over the syntax you have used. read here if you are interested in the why:

And almost forgot: can you switch your light on and off through a user interface? If not, check your Item or Thing definition.


(Vincent Regaud) #3

Mr @lipp_markus, I have copied and saved this post. It’s a beauty and I have written so many posts in the past for the same purpose that I will definitely re-use this one.
Thanks


(Pretheep K P) #4

Thanks for the reply. Fist of all apologies for not using code tags, was not aware of it. Will take care from now onwards.

I have found out the reason my issue. Seems like the internal clock time was different than the actual time. That’s why the rule was not firing when expected.
But I had set the locale and time zone correctly in openhab, then why is the internal clock different? I am using Rpi3.


(lipp_markus) #5

Glad to read that you got some further info/clarity.
You are not offering much information, which makes it hard to guess what went wrong. Would highly recommend to read this post (forgot to add this post to the list above):

  • How did you install OH on your system?
  • Which version are you running?
  • Do you see any errors in your log file?
  • Do you see any errors during start-up?
  • What do you exactly mean by "the internal clock was different from the real time? How much different, how did you notice, and what did you do?
  • As it may be RPI configuration problem, did you check the raspberry forum pages for help?

If you had not done so already, may be it is a good idea to start fresh with openHABian (see docs), this will configure most if not all of the basics for you and is likely to take care of this issue too (if it is just a misconfigured RPi).


(Pretheep K P) #6

My answers below.

  • How did you install OH on your system?
    I installed in 2 weeks back using the latest Raspian image for RPi. I use Rpi3
  • Which version are you running?
    OH2 version
  • Do you see any errors in your log file?
    No errors in log file
  • Do you see any errors during start-up?
    No errors in startup
  • What do you exactly mean by "the internal clock was different from the real time? How much different, how did you notice, and what did you do?
    The openhab2 log viwer has a time stamp for every line. That time is not equal to the actual time where I live. Its around 2 hrs 30 mins behind
  • As it may be RPI configuration problem, did you check the raspberry forum pages for help?
    I did. RPi config locale and timezone are all as per my current living location. Thats why I am wondering how its showing different in log file

(lipp_markus) #8

Did you set OH to a different timezone? In the PaperUI under System --> Configuration; scroll down to regional settings and check the timezone there.


(Pretheep K P) #9

OH is configured to the correct timezone as per where I live.


(Pretheep K P) #10

I restarted RPi now and it seems its getting the correct timezone now. I had restarted it thrice since morning and it didnt help getting timezone correct, but this time its showing the correct time.

So not sure whats wrong :frowning:


(lipp_markus) #11

Glad to hear that it works now…maybe you can mark this thread as solved