Error during rule execution (maybe due to sendCommand)

Hi Guys,

i have built a presence simulation based on Anti burglary presence simulation presented by @Mpower. I am currently using a switch (VI_autolight) to activate it. Later I would like to link it with my mobile MAC adress presence in my local wifi.
The group glight contains some dimmers and switches connected via homematic binding and some Hue devices (remark: I have an old Hue gate way but i think it cannot be cause of my issues).

I am running the following rule:

rule "AutoLight"
when
    Time cron "0 */10 * * * ?"
then
	// logInfo("AutoLight", "Auto Light Check")
	if (VI_autolight.state == ON){
		glight.members.forEach[lamp|
			lamp.sendCommand( lamp.historicState(now.minusDays(7)).state )
		]
	}
end

but i get the following messages in openhab.log:

2018-01-28 14:00:00.110 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'AutoLight': Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(org.eclipse.smarthome.core.items.Item,java.lang.String) on instance: null

I searched for the error in the forum and found some ideas of the root cause but up to now nothing solved my issue.

Does anyone have an idea?

Greetings
Sven

Maybe it would work like this:

lamp.sendCommand( lamp.historicState(now.minusDays(7)))

according to the docs: https://docs.openhab.org/configuration/persistence.html#persistence-extensions-in-scripts-and-rules the statement lamp.historicState(...) already retrieves the State of an Item at a certain point in time and you would not need to convert it by adding .state

EDIT: this is wrong, pls see next post.

Actually, it returns a HistoricItem Object. the .state is required to get at the state.

My guess is perhaps the call to historicState is failing. If you don’t have persistence set up or there is no value for that time it will return null.

Check what historicState is returning and log it out to see if that is the cause.

1 Like

@rlkoshak you mean that I need a historicState at the exact point in time it is requesting inside the database?

Meaning if it request on 15th Febraury 20:10 it will request 8th February 20:10 and if I only have an entry for that item at 20:05 and 20:40 because I use

glight*: strategy = everyChange

it will not work?

Mhh that would be bad.
I thought that the state is valid until change even if there is only and entry which is old meaning:
The database has two lines:

  • 20:07 ON
  • 23:13 OFF

Leading to give the feedback ON if I request historicState e.g. at 21:17 and OFF if I request something after 23:13.
Database is ok I checked all items entries are there but only according to the strategy above.

Ok then I need to figure out how I can check the feedback of historicState, I am not yet so deep in such things … it was the first rule I am using.

Sven

I don’t know the full behavior. It definitely needs to have a record before it at the time you ask for. However, I don’t know how far back it will search for a record before it gives up.

I can’t say whether it will work or not and it might be database dependent.

You would have to look at the code for the persistence add-on you are using it run experiments to find out.

Depending on what you are saving this value for it might make sense to use a periodically strategy (every day, every hour, etc) in addition to every change. This is particularly necessary if you plan on graphing these items using something like Gradana.

Interesting fact … logging shows … everything seems fine:
I modified the script to:

rule "AutoLight"
when
    Time cron "0 */10 * * * ?"
then
    // logInfo("AutoLight", "Auto Light Check")
    if (VI_autolight.state == ON){
        glight.members.forEach[lamp|
            // lamp.sendCommand( lamp.historicState(now.minusDays(7)).state )
            logInfo("Autolight.rule", "Geraet: " + lamp )
            logInfo("Autolight.rule", "state: " + lamp.historicState(now.minusDays(7)).state )
        ]
    }
end

and the result is:

2018-02-04 20:10:00.361 [INFO ] [marthome.model.script.Autolight.rule] - Geraet: WohnzimmerLampe_Color (Type=ColorItem, State=48,75,100, Label=Color, Category=ColorLight, Groups=[glight])
2018-02-04 20:10:00.370 [INFO ] [marthome.model.script.Autolight.rule] - state: 44,100,98
2018-02-04 20:10:00.375 [INFO ] [marthome.model.script.Autolight.rule] - Geraet: NachttischSusi_Color (Type=ColorItem, State=82,55,100, Label=Color, Category=ColorLight, Groups=[glight])
2018-02-04 20:10:00.384 [INFO ] [marthome.model.script.Autolight.rule] - state: 82,55,0
2018-02-04 20:10:00.388 [INFO ] [marthome.model.script.Autolight.rule] - Geraet: Deckenlampe_Color (Type=ColorItem, State=82,55,100, Label=Color, Category=ColorLight, Groups=[glight])
2018-02-04 20:10:00.396 [INFO ] [marthome.model.script.Autolight.rule] - state: 82,55,100
2018-02-04 20:10:00.400 [INFO ] [marthome.model.script.Autolight.rule] - Geraet: SpotsRaummitte_1_Level (Type=DimmerItem, State=50, Label=Level, Category=DimmableLight, Groups=[glight])
2018-02-04 20:10:00.409 [INFO ] [marthome.model.script.Autolight.rule] - state: 100
2018-02-04 20:10:00.413 [INFO ] [marthome.model.script.Autolight.rule] - Geraet: LichtEssecke_1_State (Type=SwitchItem, State=OFF, Label=State, Category=Switch, Groups=[glight])
2018-02-04 20:10:00.426 [INFO ] [marthome.model.script.Autolight.rule] - state: OFF
2018-02-04 20:10:00.430 [INFO ] [marthome.model.script.Autolight.rule] - Geraet: NachttischSven_Color (Type=ColorItem, State=53,94,0, Label=Color, Category=ColorLight, Groups=[glight])
2018-02-04 20:10:00.441 [INFO ] [marthome.model.script.Autolight.rule] - state: 53,94,0
2018-02-04 20:10:00.445 [INFO ] [marthome.model.script.Autolight.rule] - Geraet: SpotsSofa_1_Level (Type=DimmerItem, State=50, Label=Level, Category=DimmableLight, Groups=[glight])
2018-02-04 20:10:00.454 [INFO ] [marthome.model.script.Autolight.rule] - state: 100
2018-02-04 20:10:00.458 [INFO ] [marthome.model.script.Autolight.rule] - Geraet: LichtBuero_State (Type=SwitchItem, State=ON, Label=State, Category=Switch, Groups=[glight])
2018-02-04 20:10:00.467 [INFO ] [marthome.model.script.Autolight.rule] - state: ON
2018-02-04 20:10:00.471 [INFO ] [marthome.model.script.Autolight.rule] - Geraet: WohnzimmerLivingColors_Color (Type=ColorItem, State=47,79,100, Label=Color, Category=ColorLight, Groups=[glight])
2018-02-04 20:10:00.479 [INFO ] [marthome.model.script.Autolight.rule] - state: 47,100,92
2018-02-04 20:10:00.483 [INFO ] [marthome.model.script.Autolight.rule] - Geraet: LichtFlurOG_1_State (Type=SwitchItem, State=OFF, Label=State, Category=Switch, Groups=[glight])
2018-02-04 20:10:00.491 [INFO ] [marthome.model.script.Autolight.rule] - state: ON
2018-02-04 20:10:00.495 [INFO ] [marthome.model.script.Autolight.rule] - Geraet: LichtFlurEG_1_State (Type=SwitchItem, State=OFF, Label=State, Category=Switch, Groups=[glight])
2018-02-04 20:10:00.504 [INFO ] [marthome.model.script.Autolight.rule] - state: ON

So he returns clearly the state i need for the rule… i am at end of my knowledge. :frowning:

Greeting
Sven

OK, so we have shown that historicState is not null, if you move the sendCommand to after the logInfos does it work? It could be the case that just because we are now later in time we have moved passed a troublesome time in the DB.