Itemtype in forEach loop

How do I check itemtype in forEach loop

gLights_auto.members.forEach[lamp|
			lamp.sendCommand(ON )
		]

I want to use it for sending 100 when dimmer.

/Mike

What about using instanceof.

If (lamp instanceof DimmerType) 

Should do the trick. Got no computer in front of me. So can’t test it

Thanks

I will try this tomorrow (my wife hates when the lights goes on and off).

rule "Cleaning"
when
Item Scene_Cleaning received update
then

if (Scene_Cleaning.state == ON){
	
	postUpdate(Scene_Cleaning_Time, now)    
    
    Cleaning.members.forEach[lamp|
    	
    	If (lamp instanceof DimmerType){
    		lamp.sendCommand(100)
    		
    	} else{
    		lamp.sendCommand(ON)
    	}	
		]
          
	logInfo("Scene", "Cleaning Started")
	
	}
end

I know what you mean… :wink:

This is what I am trying.

rule "Cleaning"
when
Item Scene_Cleaning received update
then

if (Scene_Cleaning.state == ON){
	
//	postUpdate(Scene_Cleaning_Time, now)    
    
    Cleaning.members.forEach[lamp|
    	if (lamp instanceof DimmerType){lamp.sendCommand(100)}
    	else {lamp.sendCommand(ON)}]
          
	logInfo("Scene", "Cleaning Started")
	
	}
end

It does not recognize any lamp as DimmerType.

/Mike

DimmerItem

You´re right @rossko57. It´s a DimmerItem that is used here… not the Type…

Almost there
Now i am running this code

rule "Cleaning"
when
Item Scene_Cleaning received update
then
if (Scene_Cleaning.state == ON){
	var DateTime Cleanstart = now
	Scene_Cleaning_Start.postUpdate(new DateTimeType(Cleanstart.toString))    
        Cleaning.members.forEach[lamp|
    	if (lamp instanceof DimmerItem){lamp.sendCommand(100)}
    	else {lamp.sendCommand(ON)}]
          
	logInfo("Scene", "Cleaning Started")
	
	}

if (Scene_Cleaning.state == OFF && Scene_Cleaning_Start.state != null){
	Cleaning.members.forEach[lamp|
		lamp.sendCommand(lamp.historicState(Scene_Cleaning_Start.state).state)
		]
		
      	var nullValue = new DateTimeItem("trashTalking").state
        Scene_Cleaning_Start.postUpdate(nullValue)
          
	logInfo("Scene", "Cleaning Stopped")
	
	}	
	
end

But I get an error using historicState.

2017-02-08 13:50:49.330 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Cleaning': Could not invoke method: org.eclipse.smarthome.model.persistence.extensions.PersistenceExtensions.historicState(org.eclipse.smarthome.core.items.Item,org.joda.time.base.AbstractInstant) on instance: null

/Mike

The error message talks about persistence. So, what persistence have you set up?

I have mysql as persistence and is using that in presence simulation.

rule "Simulate lights"
when
    Time cron "0 0/5 * * * ?"
then
        var int days = 7
     if(Scene_Away.state == ON){
        Lights.members.forEach[light | light.sendCommand(light.historicState(now.minusDays(days)).state)]
    }
        
end		

/Mike

That’s a different group though. We have no idea what is in your Cleaning group, and if it is persisted. You might need to check what each member is before operating on it; at least put in some diagnostic logInfo so you can track the problem down - say, display the value of Scene_Cleaning_Start.state and lamp.name and suchlike

I lied

I was today testing simulation for the first time in Openhab 2 and it does not work any longer.
It worked perfectly in 1.8, is there any changes in 2.0 around historicState.

I can do Chart of both Cleaning and Light Group.

rule "Simulate lights"
when
    Time cron "0 0/5 * * * ?"
then
        var int days = 7
     if(Scene_Away.state == ON){
        Lights.members.forEach[light | light.sendCommand(light.historicState(now.minusDays(days)).state)]
    }
        
end

/Mike

What can i try more to get historicstate to work with mysql in openHAB2?

/Mike

Have you tried specifying “mysql”? (or system default -

/Mike

Like pulling teeth here. Have you tried something really simple with fixed parameters?
logInfo( “testing”, "simple - " + SomeItem.historicState(now.minusDays(2)).state.toString)

If you suspect historicState is broken (perhaps there is not enough history), try
logInfo( “testing”, "simpler - " + SomeItem.previousState.state.toString)

logInfo( "testing", "simple - " + Light_GF_Living_Window..toString)
logInfo( "testing", "simpler - " + Light_GF_Living_Window.previousState.state.toString)

Is working

And now i have this working:

rule "Cleaning"
when
Item Scene_Cleaning received update
then

if (Scene_Cleaning.state == ON){

	var DateTime Cleanstart = now
	
	Scene_Cleaning_Start.postUpdate(new DateTimeType(Cleanstart.toString))    
    
    Cleaning.members.forEach[lamp|
    	if (lamp instanceof DimmerItem){lamp.sendCommand(100)}
    	else {lamp.sendCommand(ON)}]

          
	logInfo("Scene", "Cleaning Started")
	
	}

if (Scene_Cleaning.state == OFF && Scene_Cleaning_Start.state != null){

	Cleaning.members.forEach[lamp|
		
		lamp.sendCommand(lamp.historicState(now.minusDays(1)).state)
		]
		
      	var nullValue = new DateTimeItem("trashTalking").state
        Scene_Cleaning_Start.postUpdate(nullValue)
 
          
	logInfo("Scene", "Cleaning Stopped")
	
	}	
	
end

But not

lamp.sendCommand(lamp.historicState(Scene_Cleaning_Start.state).state)

That gives error

2017-02-10 16:55:01.335 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Cleaning': Could not invoke method: org.eclipse.smarthome.model.persistence.extensions.PersistenceExtensions.historicState(org.eclipse.smarthome.core.items.Item,org.joda.time.base.AbstractInstant) on instance: null

/Mike

Okay, so [quote=“tnemrap, post:17, topic:22232”]
lamp.historicState(now.minusDays(1)).state
[/quote]
works, but not [quote=“tnemrap, post:17, topic:22232”]
lamp.historicState(Scene_Cleaning_Start.state)
[/quote]

We can guess Scene_Cleaning_Start.state isn’t digestable. I reckon this is one of those cases that shows up Joda time is not interchangeable with Openhab datetime items. Might be clues about conversion here

I’m sure I’ve seen a better guide to conversion but cannot find it now.

This code is working :slight_smile:

Now when i start scene Cleaning every lamp in group Cleaning go to full and when i stop the scene the light go back to how it was.

rule "Cleaning"
when
Item Scene_Cleaning received update
then

if (Scene_Cleaning.state == ON){
		
	Scene_Cleaning_Start.postUpdate(new DateTimeType())    
    
    Cleaning.members.forEach[lamp|
    	if (lamp instanceof DimmerItem){lamp.sendCommand(100)}
    	else {lamp.sendCommand(ON)}]

          
	logInfo("Scene", "Cleaning Started")
	
	}

if (Scene_Cleaning.state == OFF && Scene_Cleaning_Start.state != null){
	
	var DateTime cleanstarted = new DateTime((Scene_Cleaning_Start.state as DateTimeType).calendar.timeInMillis)

	Cleaning.members.forEach[lamp|
		lamp.sendCommand(lamp.historicState(cleanstarted,"mysql").state)
		]
		
   	var nullValue = new DateTimeItem("trashTalking").state
 
    Scene_Cleaning_Start.postUpdate(nullValue)
 
          
	logInfo("Scene", "Cleaning Stopped")
	
	}	
	
end

/Mike