Groovy(Script) Newbee

  • Platform information:
    • Java Runtime Environment: 17
    • openHAB version: 4.0.1

I am just a little bit frustrated, because my knowledge about abstracting from JSR223 documentation to Groovy will not work even in first lines.

import org.slf4j.LoggerFactory

LoggerFactory.getLogger("org.openhab.core.automation.examples").info("Hello, Groovy!") /OK
private org.openhab.core.Item i = itemRegistry.get('Echo_Flex_OG_Sprich') //WTF

Following error:

Script54.groovy: 5: unable to resolve class org.openhab.core.Item
 @ line 5, column 31.
   private org.openhab.core.Item i = itemRegistry.get('Echo_Flex_OG_Sprich')
1 error

Maybee someone could give me at some snippets executeable inside a independent “Script/Groovy”?
Maybee even for my requirements :wink:

if (items.getItem('Pingable_TV_Online').state == 'ON') {
  var tvOnSince = (items.getItem('Pingable_TV_Online').history.previousState(true)?.timestamp);,(tvOnSince))));

I guess everybody knows what I want to achive :wink:

Maybee another example, how to iterate over itemMembers() ?

But best would be nice links for further readings, so I can manage it myself!


This specific error is you have the wrong package. It’s org.openhab.core.item.Item.

I don’t know Groovy but I do know the JSR223 interface. So I can also say that you’ll need to use PersistenceExtensions (openHAB Core 4.1.0-SNAPSHOT API) for the persistence stuff and you’ll use BusEvent (openHAB Core 4.1.0-SNAPSHOT API) to update or command an Item.

The states of Items will be the raw Java Types instead of Strings: org.openhab.core.library.types (openHAB Core 4.1.0-SNAPSHOT API).

Iterating over the Item members I imagine will use the usual Java Collections stuff: Different Ways to Iterate an ArrayList though Groovy might have it’s own magic it can apply here.

All your interaction with OH is going to be using raw Java classes and Objects and you can find everything there is to know about those in the JavaDocs: Overview (openHAB Core 4.1.0-SNAPSHOT API). Everything else is going to be basic Groovy.

One thing to watch out for is, assuming Groovy works like Rules DSL, you can’t import when writing managed rules. Imports need to go outside the rule and when writing a Script Action or Script Condition, there is no outside the rule.

Thanks a lot!

Search can not find managed rules
Is this the right chapter ?

If you are creating anything in the UI, it’s considered “managed” as opposed to text file based.

A managed Rule is a UI rule.