Use createTimer to turn off switch

Tags: #<Tag:0x00007f616ee1d300> #<Tag:0x00007f616ee1cfe0> #<Tag:0x00007f616ee1ce28>

Hello everyone,

I’ve thrown myself into a project without any knowledge of experience of anything regarding this. A few years ago I bought an automatic cat feeder on kickstarter. The hardware is really well built and I (and my cats) love the device. Recently I realised the device has one big flaw. It seems that the feeder is an “internet of things” device that can easily turn into a useless brick when the servers of the company that build it go down. Now this company is about to go bankrupt and in a desperate attempt to save it’s skin asks us to pay a monthly/yearly subscription in order to be able to continue using the device. So, I want to hack this machine in order to be able to still use it without having to rely on this company. It would be a shame to have to toss it in the bin. As a bonus I would like to document the process so I can have a manual for others who want to attempt to do the same.

I bought a raspberry pi zero, connected a relay to it (https://www.vellemanformakers.com/product/4-channel-relay-module-vma400/) and installed the lastest openHAB on the pi with the etcher image.

The relay is attached to a physical button on the device. When the button is pushed, the motor of the device gets activated for x seconds. I created a switch as an item in the home.items file:

//Relays
Switch channel4 “petnet” {gpio=“pin:4 activelow:no initialValue:low”}

The switch works fine and I can use the openHAB app to switch it off/on. The challenge now is that I need to programm a rule in order for the switch to turn off automatically after approx 200milliseconds since that seems to be the only way to activate the motor. A push of one seconds generates nothing. I’ve spend the last two days reading on forums and in the documentation about how to do it but it seems it’s way above my head. So far I wrote following rule in home.rules:

// This is the Rules file

rule “relais off”

when petnet received command

then

if (receivedCommand == ON)

createTimer(now.plusMillis(200), [|

petnet.sendCommand(OFF)

])

end


This is fabricated from the info I could gather online but not working so cleary I’m still missing something and I can’t figure out what. I also don’t understand the use of the “var timer” so that might be what I’m missing?

Once this short push works I’m going to use the Rule engine to programm this event on specific times of the day so I can create a feeding shedule.

Thanks in advance for your help.

I suppose we should first narrow that down a bit.

Does your rule load? Look in your openhab.log for messages relating to your xxx.rules file.

Does the event that you expect to trigger your rule ever happen? Look in your events.log for messages related to the triggering Item.
I think you’ve messed up here - your Item is named channel4

It creates a “handle” so that you can do things with your timer after you’ve started it off, like cancel it. You don’t need that here, firing off an anonymous unstoppable one-shot timer is what you want.

Thank you for your answer! I was not sure whether to use “petnet” or “channel4” and tried both but nothing happened. I see following in my log concerning the rules file:

2020-06-30 16:27:49.823 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘home.rules’ has errors, therefore ignoring it: [4,6]: no viable alternative at input ‘channel4’

In the event log, all the “on” and “off” are me using the switch on the app, it never went off automatically i’m afraid (also not after changing the name to ‘channel 4’ in my items file).

2020-06-30 16:29:16.101 [ome.event.ItemCommandEvent] - Item ‘channel4’ received command OFF
2020-06-30 16:29:16.196 [vent.ItemStateChangedEvent] - channel4 changed from ON to OFF
2020-06-30 16:29:21.076 [ome.event.ItemCommandEvent] - Item ‘channel4’ received command ON
2020-06-30 16:29:21.135 [vent.ItemStateChangedEvent] - channel4 changed from OFF to ON
2020-06-30 16:29:27.221 [ome.event.ItemCommandEvent] - Item ‘channel4’ received command OFF
2020-06-30 16:29:27.285 [vent.ItemStateChangedEvent] - channel4 changed from ON to OFF

Okay, so there you can see your Item is named channel4
(The bit in “quotemarks” in your Item definition is the label, it is just text for your convenience for display)

So the rule will of course never run, and we must fix it

The clue is non-obvious, but I missed the syntax error before

Should be

rule "relais off"
when
   Item channel4 received command

Need that Item keyword

Omg, I can’t believe I got so close! I now corrected my mistakes (added the Item keyword and the correct Item name). Now when I turn the switch on it automatically switches off indeed!

I had to adapt the millis to 650 for it to work properly but after 1 week of batteling this feeder this makes me so happy!! :heart:

1 Like