How to compre time?

I try to create a good setup for a time related heating rule set. For that I have to learn how to calculate time.
Time cron I already aware about. But that is not the way I want to go. I want to use the switch/case style. Therefor I want to know If I am in the timeframe of “morning” or “lunchtime” and so on.

To play around with time is not so easy. escpecially for me as not have any experience by java and only a little with coding (former times I have used basic :slight_smile: )
So here is my startpoint:
item:

String	varZeit				"Uhrzeit"			<text>			(gMax)

rule:

val String filename = "testzeit rules"
val String zeitvergleich = "16:01"

rule "wir testen mit Zeit"
when
	Time cron "0/30 * * ? * * *"
then
	val String currentTime = String::format( "%1$tH:%1$tM", new java.util.Date )
	varZeit.postUpdate(currentTime)
	logInfo(filename, "Zeit ist " + varZeit.state)
	if (zeitvergleich > varZeit.state ){
	logInfo(filename, "Zeitvergleich ist größer " + varZeit.state)
	}
	if (zeitvergleich < varZeit.state ){
	logInfo(filename, "Zeitvergleich ist kleiner " + varZeit.state)
	}	
	
end

I know I can not do if (zeitvergleich > varZeit.state ). But how can I do that? How can I compare if one time string is before or after another?

I found already https://docs.openhab.org/configuration/rules-dsl.html#datetime-item. But I did not understand the chapter.

I hope there is a simple solution for that.

val int zeitvergleich = 961 // (hours*60)+minutes
if (zeitvergleich > now.getMinuteOfDay){

You can try this design pattern here:


Works like a charm for me.
Time comparisons can actually be pretty complicated, here some more threads:


and finally just a heads-up, some more recent changes make it harder to use older posts, if you sart getting errors/warnings, read here:

Thanks! This I was looking for.

That is the reason I am asking and looking for my own way. For me as a beginner it is really hard to find the right way in openHab2

I emphasize, I had to take it easy, take it slow and focus on one thing at a time…and spend a lot of time on the forum searching and reading posts…and I still do

My project will end up with openHab im am managing all my heatings. I bought the MAX! system. Currently I installed only three rooms and created a lot of cron rules. During that time I allready learned a lot about openHab. Now I am stopping this approach and try another one. For this I am trying now the datetime (and the issues along that way) to understand.

I allready read a lot in the forum. But I also learned not all is still usefull today anymore. The documentation is not easy for a beginner like me. E.g. I did not find the right place where the syntax is explained. Where can I read docs which are explaining me to use e.g. now.getMinuteOfDay? Or the basics like variables, operations, methods, objects and so on.

But a good community is helping me allready a lot. There are tons of good threads in the forum and very fast and polite answers for my questions. That will compensate my missing know how.

Beside the fact I would leave up the times of the heating control to max, I would suggest Design Pattern: Time Of Day

This is a good question, especially as I feel your pain with coming from a non-object oriented languages…and I have actually have no good answer; the rule engine in OH2 uses a DSL (domain specific language) that is based on Xtend which in turn is based on Java; for both of the latter two, there is plenty (which in itself can be overwhelming) of help out there, but the “fun” starts in the “based on”, which means sometimes it is just so slightly different in OH2 that the general help does not work, does not apply, etc…you don’t need Java experience luckily (speaking from experience here) but some of the concepts and some very basic knowledge will help

Either way, the concepts of objects is critical. Some of the basics for object oriented languages are captured here, maybe this will help:


(one of the many great posts from Rich, he always adds great background and information…)

And of course regarding things, items and rules, this whole chapter here is a must read (and re-read) https://docs.openhab.org/configuration/index.html

VSCode https://docs.openhab.org/configuration/editors.html is a god-sent as it will check a lot of syntax for you and more importantly, give you an idea about all the methods that can be applied to the object of interest…this alone can help a lot…and provide search terms that help further…

Your specific question regarding now… statements may be one of the more difficult topics, see my earlier comment; and in addition, as this is your heating, you may want to consider how much control you want to give OH2 on a critical system…especially while learning…most users seem to have had unintended effects that rendered OH2 non-operational, if that happens when you are not home, the consequences could be less desirable…when you search the forum, you will find contributions to this…just consider your risk tolerance and your climate (depends on where you live a total loss of heating control can have small or most severe consequences); for me personally, it is one thing if my lights act up or we have to use the hue app when OH2 is in a fritz…but heating and some other system is another thing (again depends on where you live)

As for the rest, a lot of folks here are really passionate about making the docs better; the more specific you can be in terms of what you did not understand, the better; and even more important, this is open-source, if you want to capture your learnings and add to the documentation on github, this would be more than welcome.

Wow! What an answer! Thanks a lot!

I found this link: https://www.eclipse.org/xtend/documentation/203_xtend_expressions.html#switch-expression
I will try out several operations just to learn.

And I am going to try out the Visual Studio Code. Currently I am working mainly with Notepad++

Until last week all heatings in our home have been manually. Some have been already with a simple electronic valve but nothing in a network. So if my tryouts are failing we are similar to the past. So no worry about a not working openHab so far :wink:

I am working similar to how to eat an elephant: bit by bit.... A new system needs his time to get into my brain…

Some words about me: I am living in nothern Germany. My “home” community is Zikula. That community is much smaller than openHab. But we are in a similar situation. We have switched our core from 1.x to 2.x in 2017 as well. I am an user in that community not a coder (like here). I am aware about the problematic to have a complete and always up to date documentation. If I have learned enough I hope I can give back a bit of tutorial or documentation to openHab community as well.

Not quite accurate. Xtend is based on Xtext and it runs on the Java Runtime Environment, and like a lot of similar languages like Scala, Clojure, and Groovy there is access to Java libraries. But none of these languages are based on Java. The syntax of Scala is as far from Java as Java is from Python.

This is why generic Java tutorials and the like are not that helpful. They are very different languages.

1 Like

I tried VSCode and must say it is a great help! Thanks for that hint.