"Auto off" rule with WeMo lightswitch fails

  • Platform information:
    • Hardware: RaspberryPi 3 Model B
    • OS: OpenHab2
    • Java Runtime Environment: Zulu Embedded 8.25.0.76-linux-aarch32hf, build 1.8.0_152-b76
    • openHAB version:2.2
  • Issue of the topic: Simple timer to extinguish light fails

I’m trying to create a simple rule that turns off a light I turn on after a brief delay. It’s not a particularly useful rule given the short timeout, but it’s a proof of concept. I’ve followed some sample code that seems straightforward enough, but it doesn’t work. This is the script:

var Timer timer = null

rule “On System Startup"
when
System started
then
sendMail("blah@blah.com”, “HA - Restarted”, “System restarted”)
end

rule "Auto off"
when
Item wemo_lightswitch_Lightswitch_1_0_221701K1300264 changed from OFF to ON
then
logInfo(‘Switch recognized’)
timer = createTimer(now.plusSeconds(3)[|
logInfo(‘Firing now’)
wemo_lightswitch_Lightswitch_1_0_221701K1300264.sendCommand(OFF)
timer = null
])
end

The first rule fires correctly and I get the email.

The second rule never seems to do anything. I have a simple WeMo light switch, but perhaps the id I’m using is invalid? I don’t know from where to pull the reference. I think if I did everything via config files, it would be clearer, but PaperUI makes it very easy to auto-discover and register devices. When I go to the WeMo thing, I’m just not sure what value I should set/get to populate this script.

Any hints are greatly appreciated.

First thing, please always use code fences to post code, as discourse software will change some characters and the code is more readable as well.
```

Your code goes here

```
I think you accidentally forgot a comma. Please be aware that logInfo(logger as string,message as string) has two parameters. I changed this in the rule:

rule "Auto off"
when
    Item wemo_lightswitch_Lightswitch_1_0_221701K1300264 changed to ON  // don't narrow the from down to OFF
                                                                        // could also be NULL
then
    logInfo("myRule","Switch recognized")
    if(timer !== null) timer.cancel // ensure the timer is not started yet, otherwise cancel before start it
    timer = createTimer(now.plusSeconds(3), [|  // the missing comma, just before [(
        logInfo("myRule","Firing now")
        wemo_lightswitch_Lightswitch_1_0_221701K1300264.sendCommand(OFF)
        timer = null
    ])
end
1 Like

Hi Udo,

Happy Holidays! I needed to also change the crazy name from …00264 to …00264_state. Once done, it all worked. Sorry about not putting things into code block format. I know to do that - I just spaced it. Anyway, it works fine now. Also, I switched from single to double quotes. I do a lot of Javascript programming and I’m used to single quotes. It’s also difficult to resist a semicolon at the end of every statement, but I’ll persevere. :slight_smile:

Thanks again,
Mike

Well, as far as I know, the semicolon does not harm.

In question of the naming of items, I guess the items are automatically created. the scheme is binding related, so there is no chance to find errors at this part.

Merry Christmas! :slight_smile: