Check Light Status and Toggle rule

Hi,
Today is day 3 of me playing with openhab, so a total noob,

I’m trying to set up a rule where when one device received a status update,
I want to check the current state of my light and then depending on if the light is on turn it off and vice versa,

I would think this is a very simple task but i just cannot get it to work.
below is what i have put in the rule

Item status is just a place holder to receive an mqtt command from a button (im not sure if that is the correct way to do it but…)

rule "light check"

when
	Item status received update
then
	if (mySwitch.state == ON)
	{
		sendCommand(mySwitch, ON)
	}
	else {
		sendCommand(mySwitch, OFF)
	}
end

any advice we will much much appreciated.

I think your if statement is around the wrong way. You only send the ON command if the switch state is ON.

1 Like
rule "light check"
when
	Item status received update
then
	if (status.state == ON) {
		sendCommand(mySwitch, ON)
	}
	else if (status.state == OFF) {
		sendCommand(mySwitch, OFF)
	}
end

You were checking the wrong item in your if statement. Also, it’s possible that the status variable is set to something other than ON or OFF, like Uninitialized, so I made the logic more explicit.

1 Like

This is fantastic,
having an else if worked like a charm,

now i have a it set as (cause i want the light to turn on when its off and off when its on)

Huge huge thank you, :grinning:

May its a good idea to just take a small break and relook at your work when a logical thing doesn’t want to work

rule "light check"

when
	Item status received update
then
	if (mySwitch.state == OFF)
	{
		sendCommand(mySwitch, ON)
	}
	else if (mySwitch.state == ON) {
		sendCommand(mySwitch, OFF)
	}
end