# Cannot update Timer in lambda

**URL:** <https://community.openhab.org/t/cannot-update-timer-in-lambda/129885>\
**Category:** Beginners\
**Tags:** lambda, scripts, openhab3\
**Created:** [December 10, 2021, 8:45pm UTC](https://community.openhab.org/t/cannot-update-timer-in-lambda/129885 "2021-12-10T20:45:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArkansasAtomaton](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/65b543/32.png) [@ArkansasAtomaton](https://community.openhab.org/u/ArkansasAtomaton)\
**Post date:** [December 10, 2021, 8:45pm UTC](https://community.openhab.org/t/cannot-update-timer-in-lambda/129885/1 "2021-12-10T20:45:04Z")

</div>

- Platform information:
  - Hardware: RP4 8Gb
  - OS: Bullseye
  - openHAB version: 3.2.0.M5

I am trying to create a script that uses a Timer to turn lights off if no motion has been detected for a fixed time. I created the following rule using the UI

```php
configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: DenMotionSensor
      state: OK
      previousState: ""
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      considerConditions: true
      ruleUIDs:
        - f627c72323
    type: core.RunRuleAction

```

which call this script:

```php
val String logName = "den.rules"
val Integer timeoutSeconds = 3 //Time to leave lights on after motion no longer detected
var Timer occupancyTimer = null //Tracks time until lights are to be turned OFF
  
if(occupancyTimer === null || occupancyTimer.hasTerminated()) {
  occupancyTimer = createTimer(
      now.plusSeconds(timeoutSeconds), 
      [|
          sendCommand(DenLights, OFF)
          occupancyTimer = null
          logInfo(logName, 
              "Motion stopped: timer expired, lights off")
      ]
  )
  logInfo(logName, "Motion stopped: timer started")
}
else {
    occupancyTimer.reschedule(now.plusSeconds(timeoutSeconds))
    logInfo(logName, "Motion stopped: timer rescheduled")
}

```

I get the following error: “Cannot refer to the non-final variable occupancyTimer inside a lambda expression; line 10, column 400, length 14.” The script ran in OH2.5. I can’t identify the problem and don’t know how to correct it. Any suggestions will be appreciated.  
As an aside, note that the error message refers to “column 400.”

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [December 11, 2021, 12:06am UTC](https://community.openhab.org/t/cannot-update-timer-in-lambda/129885/2 "2021-12-11T00:06:11Z")

</div>

> [@ArkansasAtomaton](#):
>
> `var Timer occupancyTimer = null `

That’s not going to work in GUI entered DSL scripts. If you need to use ‘globals’, use file based rules (where there is somewhere “outside” of the rule to create ‘globals’)

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [December 11, 2021, 4:27pm UTC](https://community.openhab.org/t/cannot-update-timer-in-lambda/129885/3 "2021-12-11T16:27:19Z")

</div>

Just want to add a note that the JSScripting add-on has a cache feature that gives one a place to store data outside the rule and share data, like timers, between scripts and rules.

To get it out great it’s only available in the JSScripting add-on but there are currently plans to eventually move the feature to core so it’s available in all languages including Rules DSL.

Good things are coming down the way.

---

<div class="post-metadata">

**Author:** ![ArkansasAtomaton](https://community.openhab.org/letter_avatar_proxy/v4/letter/a/65b543/32.png) [@ArkansasAtomaton](https://community.openhab.org/u/ArkansasAtomaton)\
**Post date:** [December 11, 2021, 8:57pm UTC](https://community.openhab.org/t/cannot-update-timer-in-lambda/129885/4 "2021-12-11T20:57:20Z")

</div>

Thanks for the comments and help.  
Does “use file based rules” mean to create and access rules like was done in OH2.5?  
I’ll look at the JSScripting to learn how to use it.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [December 13, 2021, 3:49pm UTC](https://community.openhab.org/t/cannot-update-timer-in-lambda/129885/5 "2021-12-13T15:49:14Z")

</div>

> [@ArkansasAtomaton](#):
>
> Does “use file based rules” mean to create and access rules like was done in OH2.5?

Yes.

> [@ArkansasAtomaton](#):
>
> I’ll look at the JSScripting to learn how to use it.

It’s probably too soon. Maybe in a couple weeks.
