Need help with simple rule

I’m trying to create a very simple rule. The rule consists of two switches. When one switch turns on, I’d like the other to turn off. This is the rule that I have created but it does not work:

rule "Fix Dinki Home Presence if at Work"
when Presence_Dinki_Work changed to ON
then
if (Presence_Dinki_Home.state == ON)
	{
		sendCommand(Presence_Dinki_Home, OFF)
	}
end

When I get to work, I have IFTTT send a command to myOpenHAB that toggles the Presence_Dinki_Work switch to ON. This works. I have another IFTTT recipe that sends a command to turn Presence_Dinki_Home to OFF. This usually works but sometimes it fails. The rule I’m trying to write basically says that I can’t be at two places at once so if I get to work then turn off Presence_Dinki_Home as I’m obviously not there. I’m not sure what I’m doing wrong as this seems to be pretty straight forward. Can someone spot my error?

Hi,
instead of “sendCommand” (which sends new state to the binding) use “postUpdate” (which changes state of the item).

Thanks for the tip. I had tried this before and just tried again. Unfortunately, it still does not work.

Show your item definitions, please. Without more details it will hard to find the reason. Anyway your rule looks good, you can put some “logInfo” statements to see if it switches the item in the proper moments. Also enabling and analyzing event log could help.

Can you elaborate some more on what doesn’t work? Is it that sometimes the switch doesn’t change and other times it does, it never switches, it ends up in an infinite loop of toggling back and forth?

That may indicate an error between IFTTT and my.openhab. Are there any errors noted on either service?

It is really easy to mess this type of rule up. Particularly if changing Presence_Dinki_Home triggers a similar rule to turn off Presence_Dinki_Work.

Personally, I would not use a bunch of switches and instead use a String Item to represent your location. This has the advantage that there is only one Item to represent your presence, you can easily add new locations to track (e.g. driving, at the gym, etc.) without creating a mess of new switches, and you will be freed from needing this type of rule in the first place since there is only the one Item.

Create your Item:

String Presence_Dinki "Dinki is currently at [%s]"

Then have your IFTTT recipes and other things that detect and report your current location publish where you are as text: (e.g. “home”, “work”, etc.). In your rules that care about your location you would:

if(Presence_Dinki.state.toString == "home") {
    // do home stuff
}
else if(Presence_Dinki.state.toString == "work") {
    // do work stuff
}

This is kind of a combination of what I call Separation of Concerns and Time of Day design patterns I’ve posted here:

Here’s the items:

Switch 	 	Presence_Dinki_Home					"Dinki at Home (Android)"							(Presence,Home,Persist)
Switch		Presence_Dinki_Work					"Dinki at Work (Android)"							(Presence,Persist)

I checked the events.log and when I manually toggle the switch I do see:

	Line 73658: 2016-07-26 12:32:40 - Presence_Dinki_Work_Update state updated to 2016-07-26T12:32:40
	Line 73659: 2016-07-26 12:32:40 - Presence_Dinki_Work received command OFF
	Line 73664: 2016-07-26 12:32:56 - Presence_Dinki_Work_Update state updated to 2016-07-26T12:32:56
	Line 73665: 2016-07-26 12:32:56 - Presence_Dinki_Work received command ON
	Line 73667: 2016-07-26 12:32:57 - Presence_Dinki_Work_Update state updated to 2016-07-26T12:32:57
	Line 73668: 2016-07-26 12:32:57 - Presence_Dinki_Work received command OFF
	Line 73669: 2016-07-26 12:32:57 - Presence_Dinki_Work_Update state updated to 2016-07-26T12:32:57
	Line 73670: 2016-07-26 12:32:57 - Presence_Dinki_Work received command ON

I’ve modified the script like this:

import org.openhab.core.library.types.*

rule "Fix Dinki Home Presence if at Work"
when Presence_Dinki_Work changed to ON
then
logInfo("Switch Test", "Presence_Dinki_Work changed to ON detected")
if (Presence_Dinki_Home.state == ON)
	{
		postUpdate(Presence_Dinki_Home, OFF)
		logInfo("Switch Test", "Presence_Dinki_Home ON detected then toggled OFF")
	}
end

I’ve toggled the Presence_Dinki_Work OFF then ON and still nothing. I’m not seeing anything happen and I’m not seeing any logInfo being written to openhab.log or events.log (not sure which it shows up in).

This is to combat when IFTTT doesn’t work and the switch isn’t toggled correctly. I think this may be an issue with cell data usage not working (bad area) or that myOpenHAB is having an issue at that time and it gets missed.

I have IFTTT recipes for when I connect/disconnect from my home WIFI network as well as a location based switch. This is to provide a bit of redundancy to try and best guess if I’m home or not. I also have a switch for when I am at work. If IFTTT recipe for arriving at work turns that switch on, AND any of the other switches that say I’m at home (home WIFI connect or location based) THEN turn those OFF as I can’t be at work and at home. I have not had false positives from IFTTT so if the work switch changes ON then it’s safe to say I’m not at home.

Unfortunatley, the above rule appears to do absolutely nothing.

The rule assumes that Presence_Dinki_Work is OFF before you get to work. If it is already ON the rule won’t trigger.

Try using changed, received command, or received update as the rule trigger and see if that makes a difference.

Make sure the rules file is actually being loaded and parsed correctly (you should see a line about “loading model” name of your rules file in the log when OH starts or you edit the file.)

All logInfo’s should go to “openhab.log”, so either you have logging level set too high (in logback.xml) or your rule is not loaded.
Second thing: your rule is simple and correct. I just made a quick test and this works for me - rule is fired and switches the switch.
IMO you should make sure your rule file is loaded (there should be a log entry about it in openhab.log) and you have INFO logging level configured and actually that’s it.

Sure looks like it’s loading:

2016-07-26 13:35:59.704 [INFO ] [c.internal.ModelRepositoryImpl] - Refreshing model 'presence_fix_broken_states.rules'

Changed to received command:

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*

rule "Fix Dinki Home Presence if at Work"
when Presence_Dinki_Work received command 
then
logInfo("Switch Test", "Presence_Dinki_Work changed to ON detected")
if (receivedCommand==ON AND Presence_Dinki_Home.state == ON)
	{
		postUpdate(Presence_Dinki_Home, OFF)
		logInfo("Switch Test", "Presence_Dinki_Home ON detected then toggled OFF")
	}
end

Still nothing. I haven’t restarted OH but I guess that’s my next try. I would think at the very least I’d see the logInfo statement right after the THEN execute, but I’m not getting anything in the logs.

Restart did not help :frowning:

Unless your logging is set up at a level higher than info you would see the log statement if the rule were triggering.

Are you using Designer? There could be a typo/syntax error, in your .items file perhaps.

You can try using logError which will get printed to openhab.log no matter what your logging level is set to.

In short, the problem is not with the rule as written but something else.

I just loaded the items in Designer and no errors. I’ll try changing the log level and give it another looks when I get some more time.

Thank you both for the asistance.

Again, I tried your rule in my test OH env and it worked, so changes in the rule won;t help, IMO. Since it gets loaded I would check logging level, but it seems INFO’s are logged (since you have “Refreshing model” there).
There isn’t much you can do actually, do you have other rules that are working? Please make sure all item names are correct, I just made another quick test and you can put fake item between “when” and “then” and you rule won’t get fired.
Please double check everything as such simple config just MUST work :slight_smile:

For the missing log-entry a "wild"guess. You have a space in the name of the log-entry,I don’t know but that might cause the missing log.

Does not matter. I had it as well and it worked.