Very basic rule not working

Good evening guys! I’ve got the following rule saved as ‘first.rules’ where i would like it to monitor a wall switch (with the item name of “Node01LightSwitch” and when its in a ON/1 state (defined in switch.map) to then turn on a switch with an item name of “LEDSwitch” which turns on a digitalPin. The items are all defined and specified inside of my .items file and displayed on the WebUI through the sitemap too. I feel i’m doing something completely stupid and making such a basic error but i can’t see it myself. Any help would be really appreciated, thanks in advance, Sam.

rule "lightsOn"
when
        Item Node01LightSwitch changed from OFF to ON
then
        sendCommand(LEDSwitch, ON)
end


rule "lightsOff"
when
        Item Node01LightSwitch changed from ON to OFF
then
        sendCommand(LEDSwitch, OFF)
end

I assume this should be:

sendCommand(LEDSwitch, OFF)

Also, what error message do you get?

I have just edited the post to correct my typo, thank you.

Excuse me for my lack of knowledge, where would you like me to get the error from, where about is it logged or stored to view? To figure out where i could view the errors was my next stage in the debugging process while i waited for a reply here.

It will be in openhab.log in your logs directory. Where this is depends on how you installed OpenHAB. If you installed from the Debian/Ubuntu packages then it is /var/log/openhab/openhab.log. I can’t comment on other installation methods/platforms.

Does this look like what you would expect from the last few lines?

2015-11-11 22:13:34.127 [INFO ] [penhab.io.rest.RESTApplication] - Stopped REST API
2015-11-11 22:13:40.841 [INFO ] [.o.u.w.i.servlet.WebAppServlet] - Stopped Classic UI
2015-11-11 22:15:43.641 [INFO ] [.o.core.internal.CoreActivator] - openHAB runtime has been started (v1.7.1).
2015-11-11 22:16:07.968 [INFO ] [o.o.i.s.i.DiscoveryServiceImpl] - mDNS service has been started
2015-11-11 22:16:09.748 [INFO ] [o.o.i.s.i.DiscoveryServiceImpl] - Service Discovery initialization completed.
2015-11-11 22:16:10.055 [INFO ] [.io.transport.mqtt.MqttService] - MQTT Service initialization completed.
2015-11-11 22:16:10.070 [INFO ] [o.i.t.m.i.MqttBrokerConnection] - Starting MQTT broker connection 'mymosquitto'
2015-11-11 22:16:24.127 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model 'default.items'
2015-11-11 22:16:42.180 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model 'default.sitemap'
2015-11-11 22:17:09.743 [INFO ] [penhab.io.rest.RESTApplication] - Started REST API at /rest
2015-11-11 22:17:16.227 [INFO ] [.o.u.w.i.servlet.WebAppServlet] - Started Classic UI at /openhab.app
2015-11-11 22:17:27.719 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model 'first.rules'
2015-11-11 22:30:52.416 [INFO ] [c.internal.ModelRepositoryImpl] - Refreshing model 'first.rules'
2015-11-11 22:34:33.252 [INFO ] [c.internal.ModelRepositoryImpl] - Refreshing model 'first.rules'

Yep, that’s the right log. Now execute the rule and see what is says.

By execute do you mean do what i specify in the rule to run the ‘then’ section or is there a command to start the rule?

I set up my rules following the official docs here and remember reading somewhere that rules are automatically executed. I think my tiredness is kicking in now >.<

If you just want me to perform the ‘when’ section, i did so just and got no new entries in that log.

2015-11-11 22:13:34.127 [INFO ] [penhab.io.rest.RESTApplication] - Stopped REST API
2015-11-11 22:13:40.841 [INFO ] [.o.u.w.i.servlet.WebAppServlet] - Stopped Classic UI
2015-11-11 22:15:43.641 [INFO ] [.o.core.internal.CoreActivator] - openHAB runtime has been started (v1.7.1).
2015-11-11 22:16:07.968 [INFO ] [o.o.i.s.i.DiscoveryServiceImpl] - mDNS service has been started
2015-11-11 22:16:09.748 [INFO ] [o.o.i.s.i.DiscoveryServiceImpl] - Service Discovery initialization completed.
2015-11-11 22:16:10.055 [INFO ] [.io.transport.mqtt.MqttService] - MQTT Service initialization completed.
2015-11-11 22:16:10.070 [INFO ] [o.i.t.m.i.MqttBrokerConnection] - Starting MQTT broker connection 'mymosquitto'
2015-11-11 22:16:24.127 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model 'default.items'
2015-11-11 22:16:42.180 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model 'default.sitemap'
2015-11-11 22:17:09.743 [INFO ] [penhab.io.rest.RESTApplication] - Started REST API at /rest
2015-11-11 22:17:16.227 [INFO ] [.o.u.w.i.servlet.WebAppServlet] - Started Classic UI at /openhab.app
2015-11-11 22:17:27.719 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model 'first.rules'
2015-11-11 22:30:52.416 [INFO ] [c.internal.ModelRepositoryImpl] - Refreshing model 'first.rules'
2015-11-11 22:34:33.252 [INFO ] [c.internal.ModelRepositoryImpl] - Refreshing model 'first.rules'

Would this insinuate that my ‘when’ section is not being met at all or do i need to start the rule up through a command or add it to a file entry so the OS knows to run that rule when its specifics are met?

Yeah, sorry. By execute I meant ‘fulfill the criteria in the when section’ - in this case set Node01LightSwitch from OFF to ON or vice-versa. I this is what you did above and got not output then I suggest that you add a logging statement in the then section of your rule:

logInfo("first.rules", "In rule lightsOn")

When you set the item again you should see the message printed in the log. If not, then your rule is not running.

This may be due to you attempting to trigger the rule by sending a command to the item, not a state update. How is the item defined?

See https://github.com/openhab/openhab/wiki/Rules#item—event-based-triggers for details on triggering by command. In your case, this might work:

rule "lightOn" when Item Node01LightSwitch received command ON then ... end

Right, this sounds like the solution if i’m honest. I shall try this in the morning as i’m away from the system now for the night. I will post back with my feedback early morning. I really appreciate your help, thank you.

Good morning, after having a quick look at the system again, i notice that while what you said would be true if i was attempting to trigger the rule by sending a command to the item, that isn’t what i’m doing is it?

I though that sending a command would look like this: “sendCommand[, ]”.

If you meant that i’m waiting on a command then, the wiki suggests that this will be in the format of: “Item received command []”, Which still isn’t what i’m doing, i’m waiting on the state of Node01LightSwitch to change from off to on THEN run the command, wrote with the syntax of: “Item changed [from ] [to ]”. Please correct me if i’m wrong here.

If you was to write out the when section for this rule following the syntax of “Item changed [from ] [to ]” with the Item name of Node01LightSwitch, could you please write it out for me incase i’m making some error with maybe the brackets or anything there.

While looking back through the logs, it would appear the very first time i made this rule and ran it, it actually worked for a split second, but i cant get it to even run the when section now (added the logging line you requested).

2015-11-11 20:58:52.313 [INFO ] [c.internal.ModelRepositoryImpl] - Refreshing model 'demo.rules'
2015-11-11 20:59:03.028 [WARN ] [.c.i.events.EventPublisherImpl] - given command is NULL, couldn't send command to 'LEDSwitch'

And now, without changing a thing, its decided to start working. I’m starting to get really confused with the behavior of this system now XD

1 Like

That does sound very odd.

One possible explanation is that your item state was initially undefined as you are checking both from and to states in your when clause. You could consider a rule to initialise the item on startup or restore from persistence.

I have no altered my rule to read all in one rule now so its running

"
Rule “toggleLight”
When
Item Node01LightSwitch changed
Then
IF (Node01LightSwitch.state==1) {
sendCommand(LEDSwitch, ON)
} else {
sendCommand(LEDSwitch, OFF)
}
end
"

So, i have this working (i think thats the exact code anyway, i’m away from the system at the moment). I now have to implement a situation in the code incase it can’t find a reading for the state. I think i may do this using a nested if statement into the else part. Then i meed to eradicate the delay i’m currently getting when the switch is pressed.

That seems like a more reasonable rule. Glad it’s working.

So am i Rob. Its a huge step in the right direction for the system as it stands at the moment. Now, how would you go about removing the delay here, I’ve tried the basic step of changing the delay/interval on both arduinos and the controller file but it doesn’t seem to be effecting it much. I’m thinking that maybe there is a slight delay in the radio/RPi transmitting/receiving the data, maybe even due to the switch node transmitting the temp and humidity at the same time too.

I’m going to remove everything apart from the switch on this sensor arduino and keep all 3 intervals at 200ms rather than the default 1000 and see if that changes anything. I’m slightly worried about asking too much of the raspberry pi to be reading its network 5 times per second, especially when i start to build on the amount of modules i install. What do you think?

I’m sorry, I really have no idea. I saw your other topic, hopefully you get an answer there. Perhaps try the arduino forum too.

Don’t be sorry, i really appreciate the help you gave me on my original issue. So thank you! I will try a few other forums and see what i can do.