[SOLVED] Extracting the hour from the current time

I’m lost with all the specifics around time. What I would like to do is extract the hour of the current time for a calculation. I have read through examples and the documentation for joda time to no avail so far.

var hours = DateTime.gethourOfDay()


TimeHour Now = now.TimeHour


val hours = (MyDateTimeItem.state as DateTimeType).calendar.get(Calendar::HOUR_OF_DAY)

and many variants all give some sort of error. I need some sort of pointer

var Number hours = now.getHourOfDay

But you’re mixing and confusing data type and syntaxes here so you’re obviously not very familiar with OH DSL rules programming. You need to understand that first before you embark on creating your own rules. I suggest you learn some more of that first by digging through the example section posts.
The above, for example, if put outside a rule in the global section, will be executed at rule compilation time which is not rule execution time so you will get unexpected results.

I admit newness, but have been through the examples for three days and they all seem too complicated for what I was after. I put your suggestion in and will see if it fits my situation. What I am after is turning on the 4 lights when motion is detected, unless it is too bright (working). I wanted to add a condition that between 11 pm and 6 am, only one light goes on at 50%. All I am up to is to report the hours.

rule “Motion activated light on”
when Item ZWaveNode007FGMS001MotionSensor_BinarySensor received update ON
var light = ZWaveNode007_SensorLuminance.state as Number
var Number hours = now.gethourOfDay
logWarn(“event”, hours)
if( light < 7 ) {
else if( light > 7 ) {
logWarn(“event”, “too Bright”)

I was about ready to give up Thanks for responding

You’d probably be better off using the Astro binding’s “night” trigger. It would make your rule work all year long, withotu having to adjust for the later/earlier daylight hours in the summer months. This is what I do with a motion sensor located in my master bedroom:

rule "Motion Events"
	Item Motion_MasterBedroom received update
	if (is_Night.state == ON && Motion_Master_Disable.state != ON)
		if (Motion_MasterBedroom.state == 1)
			Light_SF_Master_EnsuiteHall.sendCommand(Light_Motion_Color.state as HSBType)

And, don’t be discouraged by the Rules DSL and especially the confusion around data types/conversion. I’ve been writing openHAB rules for 4 years and I still don’t always know when to use val, var, etc. I just brute-force my way to a working rule, eventually.

Not all too bad as an advice. Also consider using VS code extensions, they already help by offering syntactically correct extension possibilities to your current code line.

1 Like

I am using the Visual Studio editor. Also in the future I may account for actual daylight hours, but I would like to get the basic idea working first.

Thanks all