[SOLVED] Presence Simulation Groovy Script

Hi All,

I’m trying to implement OH3 groovy code based on advice given by @rlkoshak in previous posts to simulate presence in the house by looping through an array of lights and setting them to historic states using persistence (I’m using rrd4j).

Here’s my groovy code:

import java.time.ZonedDateTime

// Set number of days in history to simulate
var int days = 7

// Set state of each light to its state number of "days" ago
for(light in ir.getItem("All_Lights").members){
    events.sendCommand("light", events.historicState("light",ZonedDateTime.now().minusDays(days)).state)

I get the following error in the events log:

2020-12-20 17:09:18.699 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘fc207fb83e’ failed: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.openhab.core.automation.module.script.internal.defaultscope.ScriptBusEvent.historicState() is applicable for argument types: (String, java.time.ZonedDateTime) values: [light, 2020-12-13T17:09:18.542619-05:00[America/Toronto]]

I’m using the persistence extension historicState(), but I don’t think I’m using it correctly.

Any help would be greatly appreciated!


I know nothing about Groovy, but I’m pretty sure you need to get to the Presence methods through PresenceExtensions, not events.

Thanks for the tip @rlkoshak.

I gave up trying to implement this in Groovy.
I was able to get it working using Rule DSL:

All_Lights.members.forEach[light | light.sendCommand(light.historicState(now.minusDays(7)).state.toString)]

Hope this helps someone in a similar situation.


Maybe it also would have worked by converting the historic state in Groovy to a String first using toString ? That seems to be the main difference between your Groovy and DSL rule.

@randye007 how are you triggering this rule?

@Andrew_Pawelski - Using built-in cron to trigger every 10 minutes.

Just in case someone has the same problem:
There are multiple variants of the events.sendCommand() method

  • one that takes the name of the item as String and the state as String
  • one that takes the item (object) and the state as State object

So you can either use
events.sendCommand("ItemName", "ON") or you can use
events.sendCommand(itemRegistry.get("ItemName"), OFF) using import org.openhab.core.library.types.OnOffType

In this specific case I assume leaving out the quotes on "lights" so it references the current run variable of the for loop instead a static string will do the trick.