Can't quite get started with rules

This feels like something I shouldn’t ask a web community about, it feels much more like something I should ‘man up’ and learn about before asking for help. But I’ve really struggled to find the resources that can help me understand this one.

OK. I’ve got all my items set up now. Working as they should be. I’m super happy and excited that openHAB is so cool.

And now I’m trying to write my first rule. And I’m stuck.

What I’ve been trying to do for about 3 years is control my lights based on the state of my alarm. It’s a pretty rock solid measure of ‘presence’!

So, I thought I’d start off by writing a rule to send me an email whenever the ‘armed’ state of my alarm changes. Just so I can see how and if it’s working. And then I’ll modify the rule to get it to actually do something once I’m convinced it’s working right.

So, I have this at the moment:

> import org.openhab.core.library.types.DecimalType
> import org.openhab.core.library.types.*
> import org.openhab.model.script.actions.*
> import java.lang.Math


> rule "Alarm Armed"
> when Item PARTITION1_ARM_MODE received update
> then
>     if(PARTITION1_ARM_MODE==0) {
>         logInfo("Alarm","Partition Ready") 
>         val message = "Alarm change of state\n\nReady\n\nRegards,\n\nopenHab"
>         sendMail("something@gmail.com", "Alarm Change of State", message)
>         }
>         
>     if(PARTITION1_ARM_MODE==1) {
>         logInfo("Alarm","Partition Armed AWAY")         
>         val message = "Alarm change of state\n\nArmed Away\n\nRegards,\n\nopenHab"
>         sendMail("something@gmail.com", "Alarm Change of State", message)
>         }
>     if(PARTITION1_ARM_MODE==2) {
>         logInfo("Alarm","Partition Armed STAY") 
>         val message = "Alarm change of state\n\nArmed Stay\n\nRegards,\n\nopenHab"
>         sendMail("something@gmail.com", "Alarm Change of State", message)       
>         }
>     if(PARTITION1_ARM_MODE==3) {
>         logInfo("Alarm","Partition Armed AWAY no delay") 
>         val message = "Alarm change of state\n\nArmed Stay\n\nRegards,\n\nopenHab"
>         sendMail("something@gmail.com", "Alarm Change of State", message)       
>         }
>     if(PARTITION1_ARM_MODE==4) {
>         logInfo("Alarm","Partition Armed STAY no delay") 
>         val message = "Alarm change of state\n\nArmed Stay\n\nRegards,\n\nopenHab"
>         sendMail("something@gmail.com", "Alarm Change of State", message)       
>         }
> end

I can’t seem to find any decent documentation on imports, so I’m firing into the dark there. When I change the state of the alarm I get no logging, no errors, no email. Despite the log saying Loading model 'alarm.rules' on startup.

So I’m clearly missing something major here, and I’m getting a sore head from scratching. I’d love to even see an error!

Thanks for reading.

To access the value of an openHAB item - you need to use item.state. So try if (PARITION1_ARM_MODE.state == 0).

Brilliant, thank you so much @ben_jones12 !

It’s alive! Now I’ve got an error…

> 2015-11-29 22:53:54.191 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Alarm Armed': The name 'PARITION1_ARM_MODE' cannot be resolved to an item or type.

There is an item:

> Number PARTITION1_ARM_MODE "Partition Arm Mode: [MAP(dsc.map):%d]" (DSCAlarmPartitions) {dscalarm="partition:1:partition_arm_mode"}

Ah I see it, speeling mistake…

Right, so I’ve given up trying to use gmail’s SMTP, and decided I might have more luck with the servers at my web host. So I tried their settings, but now I’m getting a load of errors:

> Caused by: javax.mail.MessagingException: Failure sending HELO command to SMTP server
>     at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.sendHelo(SMTPTransport.java:1914) ~[na:na]
>     at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.sendHandshake(SMTPTransport.java:1829) ~[na:na]
>     at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:393) ~[na:na]
>     at javax.mail.Service.connect(Service.java:265) ~[javax.mail_1.4.0.v201005080615.jar:na]
>     at javax.mail.Service.connect(Service.java:85) ~[javax.mail_1.4.0.v201005080615.jar:na]
>     at javax.mail.Service.connect(Service.java:70) ~[javax.mail_1.4.0.v201005080615.jar:na]
>     at javax.mail.Transport.send(Transport.java:94) ~[javax.mail_1.4.0.v201005080615.jar:na]

I’ve tried it with TLS and without. Password and server address are correct… Any ideas?

Below is my config for making it work with GMAIL, You do need to first generate a one time or app password inside your google account settings, you cannot just use your normal password I don’t believe. This has worked fine for me so far.

# The SMTP server hostname, e.g. "smtp.gmail.com"
mail:hostname=smtp.gmail.com

# the SMTP port to use (optional, defaults to 25 (resp. 587 for TLS))
mail:port=587

# the username and password if the SMTP server requires authentication
mail:username=me@gmail.com
mail:password=23498ASDG23FSD  <--- App password looks something like this

# The email address to use for sending mails
mail:from=me@gmail.com

# set to "true", if TLS should be used for the connection
# (optional, defaults to false)
mail:tls=true

# set to "true", if POP before SMTP (another authentication mechanism)
# should be enabled. Username and Password are taken from the above
# configuration (optional, default to false)
#mail:popbeforesmtp=
1 Like

Nice one @batfasturd got it working now.

I initially had it set up to act upon ‘receives update’

> rule "Alarm Armed"
> when Item PARTITION1_ARM_MODE receives update 

But it would send me an email every time I restarted openHAB (which is quite frequently at the moment!)

So I thought it would be better to use ‘changed’ instead. But it still emails me every time the system comes up. Is there a way of only responding to actual updates from the binding, rather than a ‘reinstatement’ from the persistence engine?

Glad it helped,
You could try specifying “when Item ITEMNAME changed to ON” or whatever the correct syntax is for your object.

Hi guys,

I still get my rules being fired whenever I restart openHAB. When the persistence is accessed and it reloads the state of items, rules that fire on updates do their thing. Is there a strategy to defeat this behaviour?

C