Little rules issue

Hi, I always have issue with this rules it should not be too complicated but I can’t find the issue

rule "iphonebattery info"
     when
      Time cron "0 0/1 * 1/1 * ? *"
       then  
 if(eli.state == ON && dom.state = ON) {
 sendCommand(iphonerefresh, ON) }
 if(iPhone5_Battery_Level == "100") {
postUpdate(iphoneelibat, ON) }
 if(iPhone7_Battery_Level == 100) {
postUpdate(iphonedombat, ON) }
    if(iPhone5_Battery_Status.state == Charging){postUpdate(iphoneelibat, ON) }
        if(iPhone7_Battery_Status.state == Charging){postUpdate(iphonedombat, ON) }
            if(iPhone5_Battery_Status.state == NotCharging && iPhone5_Battery_Level < 100 ){postUpdate(iphoneelibat, OFF) }
                 if(iPhone7_Battery_Status.state == NotCharging && iPhone7_Battery_Level < 100 ){postUpdate(iphonedombat, OFF) }

end

Hi Dominic,

I see two issues:

  1. if(eli.state == ON && dom.state == ON) { // add a second = here
  2. if you’re checking values of items, use ITEMNAME .state everywhere, you’re lacking .state on the next two IF-clauses

In general I’d say you need more context. We can’t help with the rule if there are numerous Items at play we don’t know. You also didn’t state the exact issue. The ability to drill down on an issue is important during rule development and you should practice it.

At first there are the issues @binderth mentioned.

My further suggestion would be to:

  1. Use xyz.sendCommand(ON) instead of sendCommand(xyz, ON)
  2. Add logging lines to your rule so you know where it “doesn’t work”
  3. Clean up your code, the listing above is hard to read…

You should spend some minutes studying the official documentation on rules because all of the mentioned issues are discussed there. http://docs.openhab.org/configuration/rules-dsl.html

1 Like

The original code is really hard to read with the inconsistent (random?) indentation. Thank you for using code fences though.

Here is the code with proper indentation and some other formatting to make it easier to read.

rule "iphonebattery info"
when
    Time cron "0 0/1 * 1/1 * ? *"
then  
    if(eli.state == ON && dom.state = ON) { sendCommand(iphonerefresh, ON) }
    if(iPhone5_Battery_Level == "100") { postUpdate(iphoneelibat, ON) }
    if(iPhone7_Battery_Level == 100){ postUpdate(iphonedombat, ON) }
    if(iPhone5_Battery_Status.state == Charging){ postUpdate(iphoneelibat, ON) }
    if(iPhone7_Battery_Status.state == Charging){ postUpdate(iphonedombat, ON) }
    if(iPhone5_Battery_Status.state == NotCharging && iPhone5_Battery_Level < 100 ){ postUpdate(iphoneelibat, OFF) }
    if(iPhone7_Battery_Status.state == NotCharging && iPhone7_Battery_Level < 100 ){ postUpdate(iphonedombat, OFF) }

end

So I see a bunch of random inconsistencies and, like Thom Dietrich said, we need more context. In specific:

  • What are the Item definitions for all these Items?
  • What is Charging and NotCharging? Are these supposed to be Strings? If so you MUST use quotes: e.g. “Charging”.
  • You are inconsistent in some places, using “100” in one and 100 without quotes in others. If the battery status Items are Numbers you MUST NOT use quotes. You also probably need to cast the state to Number: e.g. if(iPhone7_Battery_Level.state as Number == 100)
  • Inconsistent naming conventions used. All your postUpdate’s go to Items using all lowercase but your it statements are using the more typical camelCase with underscores. This also makes the code hard to read.
  • It is always easiest to help debug someone’s code when we know what the code is supposed to do (hence the need for more context).

I’m so sorry i ask you that this morning befor my first cofee it was just some messy night code that i had some issue with the syntax …

I know i was inconsistant but that was not my issue i had issue with some basic syntax interogation …

I’m sorry I will clean up my code re read it and if it still doesnt work i’m gonna detail it befor send it to you

How can i delete this post ?

Don’t worry about it.

1 Like

… but post your working result after you got it figured out (or still encounter issues). This way others might profit from your experience.

1 Like