Use item.state as value for another item.sendCommand

I would like to create a rule using a value from one item state to update another. Not sure how to explain it but here goes.

lightTimer2 = createTimer(now.plusMinutes(15), [|

bad_rgbw_white.sendCommand(20)

These two lines I would like to control using another item, like this

lightTimer2 = createTimer(now.plusMinutes(ctrl_led_timer2.state)

bad_rgbw_white.sendCommand(ctrl_led_nat.state)

my items contain values similar to the first lines (15 and 20)

What I’m trying to achieve is some sort of settings page for my rooms, via my sitemap file that enables me to finetune the time for my pir sensors and the dimmer value for my dimmers.

Does anyone know if this is possible to achieve ? When I try to write it in rules it fails with
1:19:41.576 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Badeværelse PIR timer': An error occurred during the script execution: Could not invoke method: org.joda.time.DateTime.plusMinutes(int) on instance: 2017-11-21T21:19:41.576+01:00

Well, beside the createTimer syntax you provide is wrong, what you have would probably work assuming that ctrl_led_nat is a Dimmer Item with a minor change.

lightTimer2 = createTimer(now.plusMinutes((ctrl_led_timer2.state as Number).intValue, [|
    bad_rgbw_white.sendCommand(ctrl_led_nat.state)
])

Pay attention to the closing parens and brackets.

If that doesn’t work you can use:

bad_rgbw_white.sendCommand(ctrl_led_nat.state as PercentType)

Again, I’ll emphasize this assumes that ctrl_led_nat is a Dimmer Item. If it is not then more work needs to be done to convert whatever type it is into a PercentType.

Thanks a lot for the help. I’ll try and post the complete rule, perhaps it will make it more clear.

I’ve tried some different approaches to what you wrote in your earlier post, I had to change

lightTimer2 = createTimer(now.plusMinutes((ctrl_led_timer2.state as Number).intValue, [|
bad_rgbw_white.sendCommand(ctrl_led_nat.state) ])

I got some errors regarding the parens

07:12:19.312 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'pir.rules' has errors, therefore ignoring it: [46,5]: missing ')' at 'lightTimer2'

So I added an extra ) I’ve tried with and without the .intValue

The error i get is as follows
07:11:05.361 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Badeværelse PIR timer': An error occurred during the script execution: The name 'ctrl_led_timer1' cannot be resolved to an item or type.

My items file
Number ctrl_bad_timer1
Number ctrl_bad_timer2

Sitemap
Frame label=“Lys” {
Slider item=ctrl_bad_dag label="Dag niveau [%d %%]"
Slider item=ctrl_bad_nat label="Nat niveau [%d %%]"
Slider item=ctrl_bad_sove label="zZz niveau [%d %%]"
Setpoint item=ctrl_bad_timer1 label="Timer led [%.0f min.]"
Setpoint item=ctrl_bad_timer2 label=“Timer rum [%.0f min.]”
}

Here’s my full rule

var Timer lightTimer = null
var Timer lightTimer2 = null

rule "Badeværelse PIR timer"
when
  Item bad_pir changed
then

  // Cancel any existing timer1
  if(lightTimer != null) {
    lightTimer.cancel
    lightTimer = null
  }

  // Cancel any existing timer2
  if(lightTimer2 != null) {
    lightTimer2.cancel
    lightTimer2 = null
  }

  // turn on lights on movement
  if(bad_pir.state == ON) {
    // turn on day lights
    if(ctrl_dag.state == ON) {
      bad_rgbw_white.sendCommand(ctrl_bad_dag.state as PercentType)
    }
    else {
      if(ctrl_sove.state == ON) {
        // turn on sleep lights
        bad_rgbw_white.sendCommand(ctrl_bad_sove.state as PercentType)
      }
      else {
        // turn on night lights
        bad_rgbw_white.sendCommand(ctrl_bad_nat.state as PercentType)
      }
    }
  }
  // Otherwise set a timer to turn off the light after defined minutes of no motion.
  else {
    // timer1 for led strip
    lightTimer = createTimer(now.plusMinutes(ctrl_led_timer1.state as Number), [|
      bad_rgbw_white.sendCommand(0)
    ])

    // timer2 turning off room lights
    lightTimer2 = createTimer(now.plusMinutes(ctrl_led_timer2.state as Number), [|
      bad_loft.sendCommand(OFF)
      bad_spejl.sendCommand(OFF)
    ])
  }
end

The error is clear and your posted .items file confirms it. You have not defined an Item named ctrl_led_timer2.

1 Like

Doh.

Sorry for wasting your time with stuff like that. Thanks a lot for the help :smiley: