Rule repeatedly executing on LDR

Ok so I have an LDR on an Arduino publishing values to mqtt and I want openHAB to turn all the house lights on at dusk when the light level drops below a certain value and to turn them off in the morning when it rises above a certain level. Problem is, because the event trigger is always true, it is constantly executing the code and spamming my rf transmitter.

I could change the update to every ‘x’ amount of minutes on the arduino side, but it would still have the same problem.

Heres the rule:

rule "Turn Lights On At Dusk"
when
Item Light_Sensor_Lounge changed
then
if (Light_Sensor_Lounge.state < 700)
{
sendCommand(Lamp_All, ON)
end

Any solutions would be amazing.

Assuming the LDR is periodically publishing value changes then that would trigger the rule on each change. You could check both the LDR state value is less than 700 and that the Lamp_All state is OFF before you turn on the lamp. The rule would still evaluate on each change but no commands would be sent when the lamp item is already in the ON state.

Youl’ll need to be a little careful with the ON/OFF thresholds (google “hysteresis” if you aren’t already familiar with it) so that the lights don’t flicker at the edge of the ON/OFF threshold.

How frequently are you publishing the LDR state? If it’s too frequent that could cause excessive rule evaluation load even if the rule isn’t sending a command.

You can simply check to see if the Lamps are already ON and only send the command to those that are OFF.

Lamp_All.members.forEach[lamp | if(lamp.state != ON) lamp.sendCommand(ON)]

However, this is really only a bandaid and you should also look into and impelment @steve1’s advice.

Yes the LDR is publishing its value every 60 seconds.

I figured I would use a dummy switch that would be used as guard. It would stop the rule from constantly executing and would also protect against the rule executing when I have voluntarily turned off the lights which would in turn drop the light level triggering the rule again.

I dont know how to write it so excuse the poor code, but the logic would go like this:

rule "Turn Lights On At Dusk"
when
Item Light_Sensor_Lounge changed
then
if (Light_Sensor_Lounge.state < 700) and dummyswitch = off
{
sendCommand(Lamp_All, ON)
sendCommand(dummyswitch, ON)
end

And then at a certain time after my usual bedtime all lights will be turned off according to the cron Timer and the dummy switch will be reset to off to prepare for the next day.

rule "Turn Lights OFF At Midnight"
when
cron Time is 12pm
then
{
sendCommand(Lamp_All, OFF)
sendCommand(dummyswitch, OFF)
end

I am not sure how to write ‘and’ in whatever code openHAB works on?

Where in the rule would I insert this?

Also how do you post code like that? Preformatted text?

I figured it out lads, thanks for your help.

There are three ways to do it.

  1. Write in the code, highlight it and press the </> button above

  2. Indent each line by at least four spaces

  3. Wrap your code in backticks:

    your code here
    

With the backtick method you can even provide the name of the language to get some additional color highlighting. Use java for Rules DSL.

```java
OH rules code here
```