How to check myItem.state?

I need some help creating my first rule… Here’s what I’d like to do:

if myItem.state = OFF
do something
if myItem.state = ON
do something else

When my rule gets triggered “OFF” gets published no matter what is stored in home/light_01/relay
Designer also complains that LIGHT_01_RELAY is undefined. How does it get defined?

Items:
Switch LIGHT_01_RELAY {mqtt=">[mybroker:home/light_01/relay:command:ON:ON],>[mybroker:home/light_01/relay:command:OFF:OFF],<[mybroker:home/light_01/relay/state:state:default]"}

Rule:
if (LIGHT_01_RELAY.state == OFF) {
		publish("mybroker","home/light_01/relay","ON")
	} else {
		publish("mybroker","home/light_01/relay","OFF")	
	}

EDIT:
It turns out that my original problem of my rule apparently not reading item.state was just a symptom. What was actually happening was the Items’ state was undefined since it wasn’t initialized at startup so the rule’s first run fell through to the ELSE. Subsequent runs also fell through because I didn’t have anything updating the items mqtt state subscription. I posted my updated files below.

Check out the documentation here: http://docs.openhab.org/configuration/rules-dsl.html
Your syntax is incorrect; your items have to be declared in a .items file, your rules in a .rules file and they have to be in the respective folders.
Here the general rule structure:

rule "rule name"
when
    <TRIGGER CONDITION1> or
    <TRIGGER_CONDITION2> or
    <TRIGGER_CONDITION3>
    ...
then
    <SCRIPT_BLOCK>
end

you have the following rule triggers:

Item <item> received command [<command>]
Item <item> received update [<state>]
Item <item> changed [from <state>] [to <state>]

All taken from the link to the documentation above.
Also review your use of the command publish. This does not exist as is in OH. I am not using MQTT myself, but your syntax is not supported in the way you write it. You will have to define a thing and an item. You can then invoke actions on an item that mostly read like:

itemname.sendCommand(your_Command_goes_here)

The documentation: http://docs.openhab.org/ will give you a clearer view.
Finally, you may want to play with the demo setup before starting on this again. It is a nice way to look at rules and other things and play around with them to gain more experience.
Hope that helps

1 Like

Nearly the same question was asked a few days back here: How to start

Good luck!

Thanks for the reply lipp_markus.
I guess I wan’t clear in my post. I do have my items defined in an items file and the IF was just a snipit of the rule. The rule is triggering as I would expect but goes to the ELSE command since I’m getting “Published Light_01_Relay OFF” written in the log.

So I have two problems ( that I know of )

  1. Is the syntax of the IF correct ?
  2. Why does OH think LIGHT_01_RELAY is undefined ?

lighting.items file

/* Lighting Items */

/* Inputs */
Switch LIGHT_01_PHYSICAL_SWITCH {channel="dscalarm:zone:60c45902:zone28:zone_tripped"} 

Switch LIGHT_01_GUI_SWITCH {mqtt=">[mybroker:home/light_01/gui_switch:command:ON:1],>[mybroker:home/light_01/relay/:command:OFF:0],<[mybroker:home/light_01/relay/state:state:default]"} 

/* Outputs */
Switch LIGHT_01_RELAY {mqtt=">[mybroker:home/light_01/relay:command:ON:ON],>[mybroker:home/light_01/relay:command:OFF:OFF],<[mybroker:home/light_01/relay/state:state:default]"}

test.rules

rule "Control LIGHT_01"
when
	Item LIGHT_01_PHYSICAL_SWITCH changed from OFF to ON
then	
		logWarn("FILE","ENTER CONTROL RULE")	
	if (LIGHT_01_RELAY.state == OFF) {
		publish("mybroker","home/light_01/relay","ON")
		logWarn("FILE","Published Light_01_Relay ON")
	} else {
		publish("mybroker","home/light_01/relay","OFF")
		logWarn("FILE","Published Light_01_Relay OFF")		
	}
end

Also review your use of the command publish. This does not exist as is in OH.

I was using publish in my rule because the docs said it was possible:
MQTT Actions

MQTT Action
Available as of openHAB 1.8

publish(String brokerName, String topic, String message)

Publish the message to topic using specified MQTT broker. The action uses the same
transport configuration settings as documented for the MQTT binding.

Thanks much for the added very helpful info, and my apology that I did not really understand your first post.
Good to know that there is a publish command, thanks for the explanation and sorry for my wrong statement,

Not seeing anything wrong with your rule;
how do you know that the state of LIGHT_01_RELAYis undefined? Your rule logic would go to the else branch also when it is ON. But maybe you have some logInfo that told you as much. If so, it appears more a MQTT and item/thing definition problem than a rule logic.
Not sure whether this is intentional, your internal rule logic makes LIGHT_01_RELAY a toggle switch: it is switched ON when it has been in the state OFF, and switched OFF when it has been in any other state.However, your rule trigger is much more specific and your rule triggers only when the status changes from OFF to ON, but your rules will not trigger if the state changes from ON to OFF. I don’t know your use case, but with such a specific trigger, why not just publish ON to your item and make another rule with the inverse trigger that publishes OFF.
As is you may face a situation where the state of your relay and the switch are inverted, which sounds confusing, but then I do not know your use case, so there might be a perfectly good reason for this.

I think I’ve got this going now. See comments in rules file.
Senario:
Momentary Switch => DSC Alarm => Envisalink => OpenHAB => MQTT => Arduino => Relay => Light

lighting.items

/* Lighting Items */

/* Inputs */
Switch LIGHT_01_PHYSICAL_SWITCH {channel="dscalarm:zone:60c45902:zone28:zone_tripped"} 
Switch LIGHT_01_GUI_SWITCH {mqtt=">[mybroker:home/light_01/gui_switch:command:ON:ON],>[mybroker:home/light_01/gui_switch:command:OFF:OFF],<[mybroker:home/light_01/relay/state:state:default]"} 

/* Outputs */
Switch LIGHT_01_RELAY {mqtt=">[mybroker:home/light_01/relay:command:ON:ON],>[mybroker:home/light_01/relay:command:OFF:OFF],<[mybroker:home/light_01/relay/state:state:default]"}

test.rules

rule "Toggle LIGHT_01_PHYSICAL_SWITCH"

/* Look for any switch changes
 * 
 * LIGHT_01_PHYSICAL_SWITCH is a momentary switch connected to a DSC Alarm Panel
 * as a normally open, non-alarm zone(zone definition=26)
 * Pushing he momentary switch shows up as two events in the events.log
 *  1. Pushing (electrically closing) the switch -> zone tripped goes from OFF to ON
 *  2. Releasing (electrically opening) the switch -> zone tripped goes from ON to OFF
 * All I care about is one of the events, look for the switch being pressed
 * and ignore the release.
 * 
 * LIGHT_01_RELAY will be connected to an Arduino board which will:
 * Subscribe to Topic => home/light_01/relay for ON|OFF commands
 * Publish to Topic   => home/light_01/relay/state to report it's ON|OFF condition
 * But since I don't have the Arduino just yet, simulate the Arduino's mqtt state
 * update with this publish("mybroker","home/light_01/relay/state","ON")
 */
when
	Item LIGHT_01_PHYSICAL_SWITCH changed from OFF to ON

then	
	if (LIGHT_01_RELAY.state == OFF) {
		sendCommand(LIGHT_01_RELAY,ON)
		publish("mybroker","home/light_01/relay/state","ON")
	} else {
		sendCommand(LIGHT_01_RELAY,OFF)
		publish("mybroker","home/light_01/relay/state","OFF")		
	}
end



rule "Toggle LIGHT_01_GUI_SWITCH"
when
	Item LIGHT_01_GUI_SWITCH received command
then	
	if (receivedCommand == ON) {
		sendCommand(LIGHT_01_RELAY,ON)
// Simulate the missing Arduino and send this mqtt publish
		publish("mybroker","home/light_01/relay/state","ON")
	}
	else {
		sendCommand(LIGHT_01_RELAY,OFF)
// Simulate the missing Arduino and send this mqtt publish
		publish("mybroker","home/light_01/relay/state","OFF")		
	}
end

Glad you got it working!!
Just a minor detail, while this works

this would is more frequently recommended:

LIGHT_01_RELAY.sendCommand(ON)

the first one invokes a rather generic sendCommand method, the second one, a sendCommand method that is more specific to your item. I am reading from those way more knowledgeable than myself that the second method is preferred and can prevent trouble.

Ok, thanks. So, where do I find this documented? Quite a bit of the Openhab2 docs I’m finding point to openhab1 wiki pages making this learning curve extra steep for me. I’m about an inch from giving up :scream:

I picked this one up from forum posts. I am sure it will eventually find its way in the documentation. Either way, the other version works too, so it is not supercritical.