DateTime, time, and Astro binding questions

First off, where can I find the docs on DateTime and “now.” ?
My search is for what the syntax is for now…as in now.currenttime

More general I’m writing a rule and (I think?) I want to compare the Astro night #start to the current time:

rule "Driveway Motion Night"
when
	Item mqtt_test changed from "No Motion" to "Motion"
then
	if(     (now >= astro:sun:myastrothing:night#start) &&
        	(now <= astro:sun:myastrothing:night#end)  && 
		(zwave_device_16500637f6a_node12_switch_binary.state == OFF) ) {
			createTimer(now.plusMinutes(30), [ | zwave_device_16500637f6a_node12_switch_binary.sendCommand(OFF) ] )
			}
end

I realize this is a lot naive, so I’ve not done things like set an item for the night begin/end or use a val for it.

(For that matter if I set an item:

DateTime Night_Begin "Day [%1$tH:%1$tM]" <sun> { channel="astro:sun:home:night#start" }```
Does it update every day? And how do I use the value in a rule? i.e. how do I compare the current time to the value of Night_Begin, i.e the value used to resolve the format statement.)

I’m guessing the night#start is a static DateTime (for a given 24 hour period) and also could be used as triggered event: a la:

when
     Channel 'astro:sun:myastrothing:night#event' triggered START
then

Astro binding get its time based on your geolocation, thus not static. Day time starts a little bit later each day for my location. If you look in the logs you will see when the binding changes from night to day.

In the rule above, when there is motion, the time is night(per the astro binding) and the zwave device == off are all true, then at whatever time it is, create a timer for 30 min. When the 30 min has expired send command off. The now.plusMinuets to create a timer does not require the Astro binding. Personally I don’t use the binding as I prefer something more along the lines of

if((now.getHourOfDay() >= 8) && (now.getHourOfDay() <= 20))

but everyone has different likes. I recommend just start using a little of everything and see what works the best.

You can find info here or the Documents page for the use of the “now.” just use the search feature.

Yes, assumed.

But, doesn’t now.getHourOfDay() return only the hours? i.e 8 in your example? (I realize that may be accurate enough in this case.)

Sure, but I haven’t been using any of this long enough to have preferences or understand subtle (or even major) differences.

Yes, I did try the docs search engine…got a nice write up on the Bose binding, link to " Working with rules and scripts" (where ctrl-f in the browser found no use of “now.”)

In addition, I’ve been trying the rule and the log file says I’m not using the astro… correctly, which I assumed was the case, since I couldn’t find any examples using it like this.

:frowning_face:

http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html

Though I highly recommend using VSCode with the openHAB extension. Then you just type “now.” and a dialog will pop up with all the available methods.

You need the Item to access the value in the Rule. You cannot check the state of a Channel within Rules. You must use Items.

Assuming you have Astro installed and still have simple mode enabled, you probably already have an Item named astro_sun_mystrothing_night.

Now also it is important to realize that now is a Joda DateTime and a DateTime Item carries a DateTimeType and the two are not super compatible. See https://www.openhab.org/docs/configuration/rules-dsl.html#transformations for how to compare and convert the two to each other. The simplest would probably be to turn the DateTimeType into a Joda DateTime.

val night = new DateTime(astro_sun_myastrothing_night.state.toString)
if(now.isAfter(night)

Again, VSCode will help you a lot here as well with autocompletion and suggestions.

Every night a few seconds after midnight.

Night_Begin.state

See above

You might learn a lot from Design Pattern: Time Of Day. Not only is it a good way to centralize these sorts of calculations, it shows examples for a lot of the time comparisons you are after.

I see Rich is replying…my advice is read what he says then read it again.

Then try to read all his replys, he’s very good.

This was suppose to come before Rich’s reply. :roll_eyes:

Not fast enough. :smiley:

I was close…:joy:

Your 17 lines, 3 links, and 7 or 8 quotes to my TWO lines.:smirk:

I really, really, really appreciate help from everyone here.

Please believe me when I say I DO search, and read, and try to figure things out before I ask. And I realize I my questions can be stated with too many words.

I find many of the Design Patterns and other threads here very good…unfortunately, I don’t get most of them on my first read, or the second, or the third. (A lot because I just don’t grok how all the pieces fit, not intuitive yet.) Typically, I have to go try things until I have an AHA! moment…but, after that the patterns and threads, etc. are obvious, but, that’s my learning disability. Maybe it’s because I have too many programming paradigms banging around in my head. :face_with_raised_eyebrow: (over the years, I’ve forgotten more than most know ever existed… :confounded: )

Rich (I think) recommended VSCODE a couple days ago, it’s probably great, and I guess I’ll have to take the time to install it…one more thing to learn during an already steep learning curve. (And, after learning untold number of development environments over the last 40+ years, you’ll understand why I shy away from learning yet another, again, my problem. :sunglasses: )

I truly hope I can get to the point where I contribute something…but, I’m afraid I have a ways to go. :thinking:

Thanks again for all the help.

I suggest that instead of just reading them and then copying and pasting, you copy the design pattern line by line the old fashioned way. This way you will really get “into” the code and get to understand it.
When I started programming (40 years ago! I was just 7) it was in BASIC and all I had was magazine with lines and lines of code that we had to copy down. I learned a lot of basic (No pun intended) programming principles that way.

1 Like

I recommend it because it makes that steep learning curve just a tad less steep. It’s an enabler. It tells you as you type what is correct and what is not correct and if you pause typing for just half a sec it tells you all the valid ways to complete what you are typing.

I’ve found there to be no greater tool to learn Rules then to just start typing in VSCode and let it tell you what it knows. It doesn’t do everything for you. You still have to understand all the parts of OH and how they work together, but at least you will know what is possible to type in Rules, know when you’ve gone wrong, and can explore the methods available on the various Objects and such.

And VSCode is really just a glorified text editor. It has a few nice features but if you can find the file menu you should be just fine. There isn’t much to learn to use it. Open the config folder, select the file from the tree on the left, start typing, save the file. That’s about it. It is nothing near as complex as Visual Studio, Eclipse, PyCharm, or any of the other IDEs that you may have experience with.

Umm…why do you assume I “just read them and then copying and pasting,…”? :face_with_raised_eyebrow:

When you started programming, 40 years ago, I’d already been programming professionally in assembly, fortran, cobol, AND BASIC for 3 years. :smirk:

BASIC, back then, had very little to document compared to something like OH. And a line of BASIC had far fewer nuances and subtleties than a line of say JAVA or Python.

And, at least for me, when learning new things like rules, half the issue is figuring out where to look to figure out what the pieces of the line mean. The rule that started this thread (that I typed every character of BTW) involves basic rule syntax, what an item is, how to reference it from a rule, what the heck is JODA time (wasn’t he the little frog guy in Star Wars? :thinking: Oh, right that Yoda, nevermind) and a DateTimeType, bindings, how to refer to bindings in different contexts (why is it astro_sun_myastrothing_night sometimes and astro.sun.myastrothing.night other time?), what a lambda is and it’s syntax, and more.

Now I’m not saying that I don’t sometimes plow ahead into coding sooner than I should, I do. But, I also find if I read too much my head gets dizzy trying to keep the details of what I read. So, I need to try stuff to figure out how it works and what the reading was talking about. In addition, I find it very difficult to learn when I can’t take very basic small steps, and a system like OH just doesn’t lend itself the the small size steps I need…so I just have to plod along and end up asking lots of questions. :grinning:

Apologies…
I just give this kind of advice regularly to anyone who expresses difficulty in learning the DSL.

Respect

Since I’m sure your are correct, so I installed VSCODE on one of my dev boxes (ubuntu).

Now two cups of coffee and two hours later :sweat_smile: :

  • did some reading and poking at my openhabianpi, looks like samba is working
  • first opened a .rules file, no highlighting…oh, need to install the OH extension
  • open .rules file, vscode says there’s an error see log file, doesn’t say where or which log file, web searches don’t help
  • vscode says I haven’t opened a folder, doesn’t indicate why I’d want open a folder, got to vscode web page read stuff
  • ok, open OH samba folder from openhabianpi, cool! code highlighting
    But what does this mean:
    image

SIgh, re-read: https://github.com/openhab/openhab-vscode#configuration

Nope, still no joy, though some popup while I type is working, same errors.

So, now three hours later…no progress at all on my home automation rules, dry eyes, and coffee jitters, this is why I procrastinated installing VSCODE. :smirk:

Lost communication with Language Server Protocol. Happens to me sometimes, you can close and reopen VScode to fix…sometimes.:expressionless:

No apologies necessary. :smile:

The Request textDocument/documentSymbol happens anytime I hover in the rule.

The Unable to open _default is when I click the OH icon in the upper right side of the VSCODE window.

It seems to know my things, and it recognizes createTimer, but it doesn’t recognize now as a key word.

I get this error when clicking the OH icon as well. I read some about it, seems to be a known issue, a month ago the OH icon would open my basicUI, guess its still not fixed.

Not sure.

I have an astro sun thing defined thus:

astro:sun:myastrothing [ geolocation="42.712662, -71.653685,88", interval=60 ] {
    Channels:
        Type rangeEvent : set#event [
            offset=-30
        ]
}

But, when the rule executes it says:
…The name ‘astro_sun_myastrothing_night’ cannot be resolved to an item or type…

But, it shows the myastrothing night channel:
image

So, I took note that you had said I might have an item

astro_sun_myastrothing_night

So I created an items file thus:

DateTime Night "Night Start [%1$tH:%1$tM]" { channel="astro:sun:myastrothing:night#start" }

Still didn’t work… :thinking:

So, I, shooting in the dark, changed:

val night = new DateTime(astro_sun_myastrothing_night.state.toString

to:

val night = new DateTime(Night.state.toString)

And…it now works.

BUT, I don’t know why. :sob:

Because except for two exceptions (Rule Triggers) Rules do not know not have access to Things and Channels.

It only knows about Items. So you had to change that line to reference the Item Night that you created.

Umm…maybe it would have been better to say: “and I don’t understand why”

Hard for me to put into words with my limited understanding of OH. But…

  • things are typically physical (or representations of phenomena, a la astro, or a Bose sound system) whose channels represent values or triggers associated with the thing.

  • items can be linked to things, but not necessarily required to have an associated thing.

  • item methods, i.e. .state.toString, are used in rules. The methods work with the current value of the item as received from it’s association with a thing. Can item methods effect the thing? For instance change it’s state.
    (I guess so since .sendCommand() changes the state of the thing. But, does the item send a command to a zwave device, then the thing responds to the zwave device saying “I changed”?)

  • why does this work:

val night = new DateTime(Night.state.toString)
rule "Test Rule"
...

But generate this in the log:

Validation issues found in configuration model 'driveway.rules', using it anyway:

Cannot reference the field 'Night' before it is defined

when the rules file is saved, and the items files has not changed and has had the definition previously?

On a different note; is it a bug, setup issue, or my misunderstanding that when I type “now.” in VSCode I don’t get any help? When I type “log” for example, I get a list of logInfo, logDebug, etc.

I also continuously get " Request textDocument/documentSymbol failed." in the VSCode OUTPUT window.