Basic rule help

Tags: #<Tag:0x00007faf7ef049b8> #<Tag:0x00007faf7ef048f0> #<Tag:0x00007faf7ef047d8>

Hi guys, I’m pretty sure I’m making a very basic or simple mistake here, but i can’t get a relay to switch on or off with a state change on a switch. I have it working perfectly in two rules, but i can’t condense it into 1 rule, if its actually possible.

The following is how i have it at the moment, and its working like a charm, perfectly:
rule “LightOn”
when
Item node1_switch1 changed
then
if (LightingCircuit1.state == OFF){
sendCommand(LightingCircuit1, ON)
}
end

rule "LightOff"
when
	Item node1_switch1 changed
then
	if (LightingCircuit1.state == ON){
		sendCommand(LightingCircuit1, OFF)
	}
end

So, the general idea here is to turn the light on or off when the switch is pressed (doesn’t matter what state the switch is, toggle the light). This allows me to control the light through the web app or phone app and still use the wall mounted switch for it without having to double press the wall switch sometimes to get the state of the switch and the state of the light to match before it works.

Thanks, Sam.

Hi,

Easy way to condense is the else statement

Cheers
Karsten

This is exactly what i have been doing but with no hope at all. Does the controller require a restart after rules/sitemap/item changes because i’ve also changed the name of one of them last night and its not working today, even after a restart. There is no logical explanation of why it isn’t working. I simple changed LightingCircuit1 to crl. Now nothing is working to do with that rule >.<

And now it starts to work with no alterations at all.

Do you have persistence configured? It is possible that upon an items file reload or openhab restart that LightingCircuit1 ends up being set to Uninitialized. So the first time you press the node1_switch1 LightingCircuit1 is not ON so it gets set to OFF, requiring a second press to get it to actually turn ON. If you have persistence then whatever was the last state your Items were in will be reloaded, avoiding the Undefined state.

I don’t think i do, however the guide i followed when installing OpenHAB may have got me to install it, i knew nothing about it back then. I may remember seeing a folder located on the Pi called persistance. So i may have it installed but not configured maybe. I shall have a quick look in the morning and let you know if i do, not that this will effect me at the moment as i’m not currently facing this issue anymore.

FYI, openHAB does come with persistence. By default it uses a round robin database. But you do have to configure it by creating a rrd4j.persist file in the configurations/persistence folder to tell it what items to persist, how often, and whether to restore the state on startup. See the wiki for details.

I’m currently in the process of setting up MYSQL persistence. Having a few little errors but I’m attempting to sort them out as we speak :smile:

I can confirm i have persistence up and running now, i’ve got it running on a mysql database. Everything looks perfect except the charts. They are huge on my desktop browser. I read on one post that it defaults at a set size of around 400ish pixels then i read somewhere else that it conforms to your browser viewpoint. I’m going to look later to see if there is a way to set a maximum size, that way it will be the same as it is now for the mobiles and tablets but on desktops it would be limited.

You can define the size of the cart through the sitemap. See the Chart document on the wiki.

Thank you for the link, saved me a few minutes. Really appreciate that!