It drives me insane

Using opehab 2.5 m2 on raspberryPi 3b.

Trying to wrap my head around a proble i have with my lighting rules.

rule “Verlichting 'savonds inschakelen bij Thuiskomst”
when
Item gPresence received command ON
then
if (Slaapstand.state == OFF && NightState.state == ON)
logInfo(“Verlichting”, “woonkamerlampen AAN”){
gWKLampen.sendCommand(85)
}
end

So I have this group of lamps, should go on only when it is dark and i am arriving home. I have to build in a different sleep mode check since my phone goes offline and comes online during the night triggering ‘me comming home’.

Now this rule triggers allways when I come home. It does not look at the sleep switch, it does not check the nightstate, it just switches on. I searched the whole site and forum but I cannot find what I am doing wrong here.

Can the ‘rules’ section on the website be expanded with more information? Info about checkin for .state on items etc. Now I have to go through a lot of forum post just to get a simple rule working. I think a few more cases and examples would really cut down requests on the forum.

I don’t recall seeing an announcement for M2. Did you mean M1 or the daily SNAPSHOTS?

Even though it’s a short Rule, please How to use code fences.

Add logging to verify that Slaapstand is indeed OFF and NightState is indeed ON. If either or both of them are in any other state then the Rule will exit without doing anything.

It could but there is nothing syntactically wrong with your Rule. If the Rule is triggering when it should then the problem is one or both of your Items in the if statement are not in the checked for state. No amount of examples will have helped with this problem.

When a Rule doesn’t work:

  • log everything
  • assume nothing
  • simplify to something that works and build it back up little by little.

Thanks for your reply. Sorry, I am using M1, made a typo.

I did check the states of both items, I added all items to my basic sitemap and can see them switch on and off. When I trigger the switches manually, the lights come on and off but no info is logged, when my other rules trigger, the loginfo is reported in the openhab log.

You helped me out with this rule before, now I changed it a bit and I just cannot find out where the mistake is. I will turn on the extra logging to see if I can find my mistake. Glad to have my syntax verified and to hear that it looks ok. I found most info in different posts and stitched it together. That does not always work out… :slight_smile:

And please post also your items.
Is the sleepswitch really a switch item?
Can it have different values than ON/OFF?

Don’t rely on the sitemap to verify the states of Items. You need to know what the Rule thinks the state of those Items are. For that you must log out the states.

The rule is only triggered when gPresence receives the command ON. You can change Slaapstand and NightState a million times but if they don’t appear in the when clause of the Rule that Rule will not trigger.

looks like you are having a syntax problems, unless I am wrong your brackets are misplaced
try

if (Slaapstand.state == OFF && NightState.state == ON)  { 
     logInfo(“Verlichting”, “woonkamerlampen AAN”)
     gWKLampen.sendCommand(85)
}

Your curly bracket { was placed after the loginfo statement.

1 Like

Shouldn’t that misplaced bracket have given an error in the log?

I like to format my rules with brackets on individual lines and lots of indenting, so that I can visualize the logic and relationships. If a rule is long, I’ll collapse it down after I know it works.

No the brackets aren’t mistplaced at all. They still encapsule code

for example

{
  logInfo("Test", "Test")
}

is valid (even if it doesn’t make sense)

I think that explains everything. Here’s @khakzoy’s code with some formatting:

if (Slaapstand.state == OFF && NightState.state == ON)
    logInfo(“Verlichting”, “woonkamerlampen AAN”)
    {
        gWKLampen.sendCommand(85)
    }

If I’m reading this correctly, the IF condition is only affecting the loginfo action, and the sendCommand is firing every time the rule goes off (because it’s outside of the IF). That would be consistent with what @khakzoy initially described, and I think the suggestion by @lipp_markus should fix it.

1 Like

Yes your syntax is wrong as explained by @lipp_markus here.

Also, you are using wrong quote characters in the logInfo line. Use these: "

That’s why the rule does not trigger. You should have seen a syntax error when you created it.

Sorry for my late replies. Yeah, this fixes my problem of the rule not firing. I changed the {} location and now the rule fires as intended.

Thanks for the patience and solution.