If statement of Switch is not Working

Hello everyone,

I have a really simple rule, which should be switching on some lights at dusk and turn them of at dawn. For this purpose I’m checking the state of a switch, which indicates the current state - day or night. During Night it is set to “ON” by another rule or manually through the dashboard for testing purposes.

The rule is as follows:

rule "morgens aus/abends an"
when
	Item switchNight changed
then
	if (switchNight.state.toString == "ON") {
			lr_scene.sendCommand(nmbNightAuto.state)
			logInfo("Nacht", "Lampen an, weil dunkel: " + nmbNightAuto.state)
	}
	else {
			lr_scene.sendCommand(OFF)
			
	}
end

The Item.state.toString ist just a desperate attempt to get this going. logInfo("Switch Value", "" + switchNight.state) returns either ON or OFF as expected. The if clause in the rule still always goes to else. As soon as I try to use the value otherwise (like in the unnecessary cast to string above), it returns the following error message:

2021-09-02 16:20:23.163 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘lichtsteuerung-4’ failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.BusEvent.sendCommand(org.openhab.core.items.Item,java.lang.String) on instance: null in lichtsteuerung

What am I doing wrong here? Even with the if minimized to if (switchNight) { it does not work.

The error message is all about sendCommand being given something it cannot digest. That frequently pops up when attempting to use another Item’s state; states are not commands, even if they look like one. State ON is not command ON.
Simplest get out is to command using string, which forces sendCommand to parse into whatever object type it is looking for.
Technically correct way would be to cast state as OnOffType or whatever.

Sorry if it came accros as problem with sendCommand, those do work. What does not is the if statement - no matter how I use the trigger, it alyways goes to else. There are no commands being sent.

Edit: To be more clear:

val bool = (switchNight == "ON") as Boolean
logInfo("switch", switchNight.state + "")
logInfo("DEBUG", bool + "")

2021-09-02 17:45:31.371 [INFO ] [org.openhab.core.model.script.switch] - OFF
2021-09-02 17:45:31.373 [INFO ] [org.openhab.core.model.script.DEBUG ] - false
2021-09-02 17:45:32.223 [INFO ] [org.openhab.core.model.script.switch] - ON
2021-09-02 17:45:32.225 [INFO ] [org.openhab.core.model.script.DEBUG ] - false

Whereas:

val bool = (switchNight.state.toString == "ON") as Boolean
logInfo("switch", switchNight.state + "")
logInfo("DEBUG", bool + "")

2021-09-02 17:48:31.179 [INFO ] [org.openhab.core.model.script.switch] - OFF
2021-09-02 17:48:31.181 [INFO ] [org.openhab.core.model.script.DEBUG ] - false
2021-09-02 17:48:31.768 [INFO ] [org.openhab.core.model.script.switch] - ON
2021-09-02 17:48:31.772 [INFO ] [org.openhab.core.model.script.DEBUG ] - true

But upon execution of the rule

Script execution of rule with UID ‘lichtsteuerung-4’ failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.BusEvent.sendCommand(org.openhab.core.items.Item,java.lang.String) on instance: null in lichtsteuerung

Edit 2: I could solve the problem now, passing the bool val to the if-statement. I still don’t understand, why it doesnt work the other way round.

rossko57’s point is based on the error the sendCommand is in fact not working. It’s not completely apparent which sendCommand is not working (you have two) but the error is coming from a call to sendCommand.

We can’t really ignore that and since you present that as evidence the rule is not working it is somehow related to the problem at hand.

Well of course. switchNight isn’t a String. It’s an Item. And switchNight.state isn’t a String, it’s an OnOffType.

ON != "ON"

A correct test would be either

val bool = (switchNight.state == ON) as Boolean
logInfo("switch", switchNight.state + "")
logInfo("DEBUG", bool + "")

Notice how we compare the state of the Item switchNight to the OnOffType ON.

The other approach is as you’ve shown, convert everything to a String and compare the strings.

Side Note: the as Boolean is redundant.

That seems to indicate there is a problem with the second call to sendCommand. So does lr_scene exist as an Item? Is it a Switch Item or Dimmer Item or Color Item?

That is what is generating the error in the logs.

I recommend figuring out that issue first because it may be a sign that there is something more significant going on which is causing the if statement to behave unexpectedly.

But for completeness, assuming switchNight is the name of a real Item and that Item is a Switch Item one of these two should work:

if(switchNight.state == ON) {

if(switchNight.state.toString == "ON") {

If switchNight is not an Item or it’s not a Switch Item then neither of those will work.

1 Like

Sometimes “this rule must work but doesn’t” comes down to editor not saving where/when you expected, or to syntax in some other rule in the same file breaking the structure. Check your openhab.log for “rules loaded” messages.

1 Like