The name 'now' cannot be resolved to an item or type

I’m getting errors using now.

Rules are

rule Radio61
when
Item Radio_6102 received update
then
time_change_61.postUpdate( Radio_6102.state.toString + " dB @ " + now.toString(“HH:mm”) +" on "+ now.toString(“dd/MM/yyyy”))
end

=====================================================
rule “Check Time”
when
Time cron “18 * * * * ?”
then

var Number HF  = now.getHourOfDay()
var Number MF = now.getMinuteOfHour()

TimeofDayMinutes.sendCommand (HF*60+MF)

end

Any ideas?

There seems to be a problem with the latest snapshot, is that the case?

You second rule could be simplified to:

rule “Check Time”
when
    Time cron “18 * * * * ?”
then
    TimeofDayMinutes.sendCommand(now.getMinuteOfDay() as Number)
end

Yes, I confirm this issue on build 1519

The issue has been solved with latest snapshot 1522 (see https://github.com/openhab/openhab-core/issues/553).

Thanks :slight_smile:

Thanks. The rule was more complex and did some calculations and had some logic using the hour - but have since simplified it :slight_smile:

My scenario is a bit different than this thread is talking about.

I’ve been getting these on every clean startup of OH since I started working with OH (2.3 and 2.4).

NOW does work but sometimes, I have to push the rules file again after it starts up. The bottom line it’s a timing issue.

Is there a more elegant way to catch the issue on startup to have it try to re-initialize local rule variables tied to NOW when this happens below? It’s more of a nuisances remembering I have to re-push the rule file again.

2019-08-22 10:14:13.842 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'default.rules'
2019-08-22 10:14:27.855 [WARN ] [me.internal.engine.RuleContextHelper] - Variable 'currMonth' on rule file 'default.rules' cannot be initialized with value '<XFeatureCallImplCustom>.getMonthOfYear': The name 'now' cannot be resolved to an item or type; line 93, column 28, length 3
2019-08-22 10:14:27.858 [WARN ] [me.internal.engine.RuleContextHelper] - Variable 'currDay' on rule file 'default.rules' cannot be initialized with value '<XFeatureCallImplCustom>.getDayOfMonth': The name 'now' cannot be resolved to an item or type; line 94, column 27, length 3
2019-08-22 10:14:27.860 [WARN ] [me.internal.engine.RuleContextHelper] - Variable 'currDayofWeek' on rule file 'default.rules' cannot be initialized with value '<XFeatureCallImplCustom>.getDayOfWeek': The name 'now' cannot be resolved to an item or type; line 95, column 31, length 3
2019-08-22 10:14:27.871 [WARN ] [me.internal.engine.RuleContextHelper] - Variable 'currHourVariable' on rule file 'default.rules' cannot be initialized with value '<XFeatureCallImplCustom>.getHourOfDay': The name 'now' cannot be resolved to an item or type; line 96, column 33, length 3
2019-08-22 10:14:27.880 [WARN ] [me.internal.engine.RuleContextHelper] - Variable 'currMonthString' on rule file 'default.rules' cannot be initialized with value '<XFeatureCallImplCustom>::format(<XStringLiteralImpl>,<XFeatureCallImplCustom>)': The name 'String' cannot be resolved to an item or type; line 98, column 34, length 6
2019-08-22 10:14:27.886 [WARN ] [me.internal.engine.RuleContextHelper] - Variable 'currDayString' on rule file 'default.rules' cannot be initialized with value '<XFeatureCallImplCustom>::format(<XStringLiteralImpl>,<XFeatureCallImplCustom>)': The name 'String' cannot be resolved to an item or type; line 99, column 33, length 6
2019-08-22 10:14:27.889 [WARN ] [me.internal.engine.RuleContextHelper] - Variable 'Cal_Date0' on rule file 'default.rules' cannot be initialized with value 'now': The name 'now' cannot be resolved to an item or type; line 102, column 29, length 3
2019-08-22 10:14:27.891 [WARN ] [me.internal.engine.RuleContextHelper] - Variable 'Cal_Date1' on rule file 'default.rules' cannot be initialized with value '<XFeatureCallImplCustom>.plusDays(<XNumberLiteralImpl>)': The name 'now' cannot be resolved to an item or type; line 103, column 29, length 3
2019-08-22 10:14:27.900 [WARN ] [me.internal.engine.RuleContextHelper] - Variable 'Cal_Date2' on rule file 'default.rules' cannot be initialized with value '<XFeatureCallImplCustom>.plusDays(<XNumberLiteralImpl>)': The name 'now' cannot be resolved to an item or type; line 104, column 29, length 3
2019-08-22 10:14:27.908 [WARN ] [me.internal.engine.RuleContextHelper] - Variable 'Cal_Date3' on rule file 'default.rules' cannot be initialized with value '<XFeatureCallImplCustom>.plusDays(<XNumberLiteralImpl>)': The name 'now' cannot be resolved to an item or type; line 105, column 29, length 3
2019-08-22 10:14:27.920 [WARN ] [me.internal.engine.RuleContextHelper] - Variable 'Cal_Date4' on rule file 'default.rules' cannot be initialized with value '<XFeatureCallImplCustom>.plusDays(<XNumberLiteralImpl>)': The name 'now' cannot be resolved to an item or type; line 106, column 29, length 3

Best, Jay

Your rule is triggered through System started? Maybe you’ll have to delay the execution.

Correct and I do have that delay below. These NOW errors in the Starting Rule starts even before it starts processing the actually System Started rule. It’s basically once the rule file is loaded, it happens immediately since the VAR’s are sitting at the top before the System Started rule gets hit.

	    	Thread::sleep(20 * 5000)	// let persistance finish restoring

Best, Jay

So please show the rules file :slight_smile:

I only have 1 rule file which is 18k lines long . . . Here’s the top of it.

import java.util.List
import java.util.ArrayList
import org.eclipse.xtext.xbase.lib.Functions
import java.util.concurrent.locks.ReentrantLock
import org.eclipse.smarthome.model.script.actions.Timer

val String			ruleFilename = "default.rules"				// Used to keep track of which rule file is parsed
var String			reminder_Jay = null							// Alexa Last Heard Reminder
var String			reminder_LivingRoom = null					// Alexa Last Heard Reminder
var String			reminder_Bedroom = null						// Alexa Last Heard Reminder
var String			reminder_Basement = null					// Alexa Last Heard Reminder
var String			reminder_Gym = null							// Alexa Last Heard Reminder
var String			reminder_Loft = null						// Alexa Last Heard Reminder
var String			reminder_Parker = null						// Alexa Last Heard Reminder
var String			reminder_Ryan = null						// Alexa Last Heard Reminder
var String			reminder_Tricia = null						// Alexa Last Heard Reminder
var String			reminder_Garage = null						// Alexa Last Heard Reminder
var String			reminder_GuestRoom = null					// Alexa Last Heard Reminder
var String			reminder_LexusSedan = null					// Alexa Last Heard Reminder

var Timer			reminder_Jay_tAlive = null					// Alexa Timer
var Timer			reminder_LivingRoom_tAlive = null			// Alexa Timer
var Timer			reminder_Bedroom_tAlive = null				// Alexa Timer
var Timer			reminder_Basement_tAlive = null				// Alexa Timer
var Timer			reminder_Gym_tAlive = null					// Alexa Timer
var Timer			reminder_Loft_tAlive = null					// Alexa Timer
var Timer			reminder_Parker_tAlive = null				// Alexa Timer
var Timer			reminder_Ryan_tAlive = null					// Alexa Timer
var Timer			reminder_Tricia_tAlive = null				// Alexa Timer
var Timer			reminder_Garage_tAlive = null				// Alexa Timer
var Timer			reminder_GuestRoom_tAlive = null			// Alexa Timer
var Timer			reminder_LexusSedan_tAlive = null			// Alexa Timer

var Timer			Node4_tAlive = null							// garage 6 sensor
var Timer			Node5_tAlive = null							// attic 6 sensor
var Timer			Node6_tAlive = null							// kitchen 6 sensor
var Timer			Node7_tAlive = null							// basement 6 sensor
var Timer			Node8_tAlive = null							// loft 6 sensor
var Timer			Node9_tAlive = null							// garage door
var Timer			Node10_tAlive = null						// foyer 6 sensor
var Timer			Node11_tAlive = null						// laundry water sensor
var Timer			Node12_tAlive = null						// sump pump water sensor
var Timer			Node13_tAlive = null						// furnace water sensor
var Timer			Node14_tAlive = null						// mailbox door sensor
var Timer			Node15_tAlive = null						// Garage repeater sensor
var Timer			Node16_tAlive = null						// UpStairs repeater sensor
var Timer			Node17_tAlive = null						// Basement repeater sensor
var Timer			Node18_tAlive = null						// MainFloor repeater sensor
var Timer			Node19_tAlive = null						// garage door 2

var Timer			OfflineTimer_tAlive = null					// Tricias Echo Account Online
var DateTime		ConnectionLostTime = null					// Tricias Echo Account Connection Lost

var Timer			OfflineTimer2_tAlive = null					// Michaels Echo Account Online
var DateTime		ConnectionLostTime2 = null					// Michaels Echo Account Connection Lost

var Timer			OfflineTimer3_tAlive = null					// Lexus Sedan Echo Account Online
var DateTime		ConnectionLostTime3 = null					// Lexus Sedan Echo Account Connection Lost

var Timer			GarageDoorUp_tAlive = null					// Garage Up Timer 10+ minutes
var Timer			GarageDoorTricia_tAlive = null				// Garage Up with Tricia Leaving

var Timer			BasementHumid_tAlive = null					// Humidity Re-Check
var Timer			LoftHumid_tAlive = null						// Humidity Re-Check
var Timer			GarageHumid_tAlive = null					// Humidity Re-Check
var Timer			AtticHumid_tAlive = null					// Humidity Re-Check
var Timer			KitchenHumid_tAlive = null					// Humidity Re-Check

var Timer			KitchenMotionDelay_tAlive = null			// Hue Motion Kitchen Sensor
var Timer			LoftMotionDelay_tAlive = null				// Hue Motion Loft Sensor
var Timer			BasementMotionDelay_tAlive = null			// Hue Motion Basement Sensor
var Timer			JaysMotionDelay_tAlive = null				// Hue Motion Jays Office Sensor
var Timer			HallwayMotionDelay_tAlive = null			// Hue Motion Hallway Sensor

var Timer			HallwayWemoDelay_tAlive = null				// WeMo Hallway
var Timer			JaysWemoDelay_tAlive = null					// WeMo Office
var Timer			LaundryWemoDelay_tAlive = null				// WeMo Laundry

var Timer			BasementHumidifierDelay_tAlive = null		// WeMo Basement Dehumidifier Plug

var Timer			AtticTemp_tAlive = null						// Temperature Re-Check
var Timer			AtticWinterTemp_tAlive = null				// Temperature Re-Check
var Timer			GarageWinterTemp_tAlive = null				// Temperature Re-Check

var String			HIKSwitchFrontDoor = 'N'					// Camera motion switch to slow down rule
var	String			HIKSwitchGarageDoor = 'N'					// Camera motion switch to slow down rule

var HSBType 		Blacklight = ("63,3,0")						// Black - catch all

var String			DummyImageURL		= "http://192.168.0.230:8080/static/images/noimage.jpg"
var String			DummyTrack			= "Nothing Queued Up"

var Number			currMonth			= now.getMonthOfYear
var Number			currDay 			= now.getDayOfMonth
var Number			currDayofWeek		= now.getDayOfWeek		//	Mon = 1, Tue = 2, Wed = 3, Thr = 4, Fri = 5, Sat = 6, Sun = 7
var Number			currHourVariable	= now.getHourOfDay		//  Variable used with an item for hour persistence

var String			currMonthString		= (String::format("%02d", currMonth))					// Used for 2 digit Month
var String			currDayString 		= (String::format("%02d", currDay))						// Used for 2 digit Day
var String			TodaysDateTime_String

var	DateTime		Cal_Date0			= now
var	DateTime		Cal_Date1			= now.plusDays(1)
var	DateTime		Cal_Date2			= now.plusDays(2)
var	DateTime		Cal_Date3			= now.plusDays(3)
var	DateTime		Cal_Date4			= now.plusDays(4)

Best, Jay

Ah. You can’t use functions outside a rule. Instead, you have to use a rule to update the var.

So I just remove the functional stuff from those variables like this?

var Number			currMonth		
var Number			currDay 			
var Number			currDayofWeek	
var Number			currHourVariable	

var String			currMonthString						// Used for 2 digit Month
var String			currDayString 						// Used for 2 digit Day
var String			TodaysDateTime_String

var	DateTime		Cal_Date0			
var	DateTime		Cal_Date1		
var	DateTime		Cal_Date2		
var	DateTime		Cal_Date3			
var	DateTime		Cal_Date4			

I’m assuming setting NULL and basic strings are OK or am I wrong?

var String			HIKSwitchFrontDoor = 'N'					// Camera motion switch to slow down rule
var	String			HIKSwitchGarageDoor = 'N'					// Camera motion switch to slow down rule

var HSBType 		Blacklight = ("63,3,0")						// Black - catch all

var String			DummyImageURL		= "http://192.168.0.230:8080/static/images/noimage.jpg"
var String			DummyTrack			= "Nothing Queued Up"

Best, Jay

Constants is fine.

Assigning functions to globals is generally pointless, as they get evaluated once at load time only. You’d usuaĺly be more interested in e.g. “now” at rule execution time, which might be days later.

I do have that logic in place everywhere; I check to see if those variables aren’t null/NULL then I update them before using them everywhere in my RULE file.

Example of it

		if (systemStarted.state != ON && currHourVariable !== null && currHourVariable != NULL && currHourVariable != UNDEF && currHourVariable != '' && gInternet.state == ON) {

			currHourVariable = now.getHourOfDay
			currHour.postUpdate(currHourVariable)
			currDayofWeek = now.getDayOfWeek 
			currMonth = now.getMonthOfYear 
			currDay = now.getDayOfMonth

			currMonthString	= (String::format("%02d", currMonth))					// Used for 2 digit Month TRIGGERED by Rule
			currDayString 	= (String::format("%02d", currDay))						// Used for 2 digit Day TRIGGERED by Rule
		}

Best, Jay