Rules DateTime does not run

Hi

I am trying rules to basically switch on some lights when Plex starts playing. I am having trouble when I add or manipulate datetimes

In the OpenHab designer I get errors for now entries and DateTime however not sure what I’m doing wrong. It could be something very simple.

Also rules file does not load i think when there is such entries. Without them I do get some logging.
I have openhab 1.8.2 on a raspberry pi

Any pointers would be greatly appreciated.

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*

var Number counter = 1

/**
 * Loads on startup
 */
rule Startup
when 
	System started
then
	logDebug("openhabrules", "OpenHab Rules Load")
end

/**
 * When plex starts playing lights go on
 */
rule MovieLightsOn
when
	Item PlexTVStatus changed to Playing
then
	
	logDebug("openhabrules", "Plex Started Switch Lights")
	var year = now.getYear
    var month = now.getMonthOfYear
    var day = now.getDayOfMonth
    var DateTime sunset = parse(year+"-"+month+"-"+day+"T"+AASunset_Time.state)
    
	if ( now.isAfter(sunset)) {
		logDebug("openhabrules", "Plex Started Switch Lights")
		sendCommand("livingrlamp1", "ON")
		sendCommand("livingrlamp2", "ON")
	}
	else {
		logDebug("openhabrules", "Plex Started Still Daylight")
	}
end

I don’t think you need to import:

import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*

There are classes and methods in Joda Time that can be used to manipulate dates and times more simply, but isn’t AASunset_Time.state already set to today’s sunset time, so there would be no need to use now or combine it with AASunset_Time.state?

…adding, use this to define sunset:

val DateTime sunset = new DateTime((AA_Sunset_Time.state as DateTimeType).calendar.timeInMillis)

Thanks, you are right since the sunset time has already the date

My root issue was different though, rules file was saved in windows format CRLF as EOL.

Converted to Unix format and it worked like a charm!

Thanks

1 Like