[SOLVED] Can't access state (or not correctly evaluated)

Hello,

  • Platform information:
    • Hardware: Cubietruck - ARMv7 - 2 CPU - 2Gb RAM
    • OS: Armbian (Debian Stretch)
    • Java Runtime Environment: openjdk version “1.8.0_181”
    • openHAB version: 2.3.0-1

As Items, I have:

Number OeilDeMoscou_Luminosity "Oeil de Moscou Luminosité [%.0f]" <light> {channel="zwave:device:a018f911:node2:sensor_luminance"}
Switch Wallplug_Garage "Wallplug Garage" <poweroutlet> {channel="zwave:device:a018f911:node3:switch_binary"}
Switch OeilDeMoscou_Motion "Oeil de Moscou Motion" <camera> {channel="zwave:device:a018f911:node2:alarm_motion"}

As rules, I have :

rule "Wallplug_Garage ON"
when
  Item OeilDeMoscou_Motion changed from OFF to ON or
then
  var Number hour = now.getHourOfDay
  var lightState = 'OFF'

  if(hour >= 9 && hour <= 17) lightState = 'OFF'
  else if (hour <9 || hour > 17) lightState = 'ON'

  if (OeilDeMoscou_Luminosity.state < 80) { lightState = 'ON' }

  Wallplug_Garage.sendCommand(lightState)
end

rule "Wallplug_Garage OFF"
when
  Item OeilDeMoscou_Motion changed from ON to OFF
then
  Wallplug_Garage.sendCommand(OFF)
end

As you may have guessed, I would like to switch on light only if luminosity is below 80. This does not seem evaluated as the lights were switched on with a value upper than 80. For the rest of the rule, it works well.

If I try to do a

logInfo("test123", OeilDeMoscou_Luminosity.state)

Log says it’s null.

What did I miss ?

Thanks,
Nicolas

null or NULL? They are not the same thing.

From my perspecitve, it’s more the object does not exist thant it has a NULL value - will get the log later today to check once I’m back home to confirm this.

Exact log is:

==> /var/log/openhab2/openhab.log <==
2018-10-02 21:06:20.240 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Wallplug_Garage ON': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null

The code posted above does not have a logInfo statement in this Rule. It’s hard to help debug a Rule when the code posted is not the code generating the error.

Errors like this often point to a type problem. Are you trying to pass primitives to logInfo?

Log is when I add this in the rule:

Whole rules file is:

rule "Wallplug_Garage ON"
when
  Item OeilDeMoscou_Motion changed from OFF to ON
then
  var Number hour = now.getHourOfDay
  var lightState = 'OFF'

  if(hour >= 9 && hour <= 17) lightState = 'OFF'
  else if (hour <9 || hour > 17) lightState = 'ON'

  logInfo("test123", OeilDeMoscou_Luminosity.state)
  if (OeilDeMoscou_Luminosity.state < 80) { lightState = 'ON' }

  Wallplug_Garage.sendCommand(lightState)
end

rule "Wallplug_Garage OFF"
when
  Item OeilDeMoscou_Motion changed from ON to OFF
then
  Wallplug_Garage.sendCommand(OFF)
end

My idea was to see the value of the state of OeilDeMoscou_Luminosity to be sure I cas comparing something to 80.

OK, this is an annoying type problem. Because there is nothing else to tell the Rules DSL that you want to use OeilDeMoscou_Luminosity.state as a String it is treating it as some other type (probably State) and failing because logInfo requires two Strings.

logInfo("test123", OeilDeMoscou_Luminosity.state.toString)
1 Like

Ok, then it works:

==> /var/log/openhab2/openhab.log <==
2018-10-02 21:14:45.108 [INFO ] [lipse.smarthome.model.script.test123] - 0

So maybe I need to improve the if statement to be sure I compare a number - which is supposed to be.

If I do

if (OeilDeMoscou_Luminosity.state as Number < 80) { lightState = 'ON' }

log will ouput

2018-10-02 21:17:23.522 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'lights.rules' has errors, therefore ignoring it: [12,49]: no viable alternative at input '80'

Ok found with a previous answer of you that I need to add () as below:

if ((OeilDeMoscou_Luminosity.state as Number) < 80) { lightState = 'ON' }

Thanks a lot for your help !