Noob question about rules

i have a light that works only when is switched on
so if the light is off (state off) and i turn on the light go on
if i change state to off the light is still on
if i change state to on the light go off
if i change to off the light stay to off
i try to make a rule like that

var item = items.getItem("Luce")
if (item.state == "OFF"){
  item.sendCommand("ON")
  item.sendCommand("OFF")
}

but ofc doesn’t work since it autotrigger the rule
i read about use a var like
var timer = now
and use that for see the last activation, but if i initialize it at the start doesn’t works since is always = now…
and this came my question
i read a lot of post with the code formatted in this way:
var t = now;
rule xyz
when condition
then fun()
end

but i don’t understand how i can create this kind of rules
if i click on “code” of the rule i get another format like that

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: Luce
    type: core.ItemCommandTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript;version=ECMAScript-2021
      script: |
        var item = items.getItem("Luce")
        if (item.state == "OFF"){
          item.sendCommand("ON")
          item.sendCommand("OFF")
        }
    type: script.ScriptAction

and if i change this format with the other it doesn’t works… so how i can create that kind of rule?

Welcome to the forums!

The rules that you see written out with the pattern of

rule XXX
when
  something
then 
  some action
end

are using the OH scripting language rulesDSL and that particular format is only if you are creating the rules in special rules text files with a file extension of .rules and placed in the conf directory where OH knows to look for these special files.

The rulesDSL language can still be used for rules if you are creating the rules using the UI. You cannot, however, just past the whole thing over to the UI because the UI processes the various peices differently. Just pay attention to the similarity between the UIs rules dialog and the text rules structure. In the text file format there’s a section that says when (this is the rule trigger): on the rules dialog the second block of inputs are title When so this is the same part. You’ve already made a rule that runs (even if it doesn’t work exactly the way you wanted) so you understand how to put a rule trigger in this section.

The next block on the UI dialog is labeled Then: this is place you will put the code that in the text based file is found between the then and end keywords. Just like you have already done, create a user-defined script action in the Then section but make sure that you choose rulesDSL as the language for that particular script.

I suspect, however, that you will find this doesn’t solve the problem you are hoping to solve. The example rule that you have started includes a variable definition before the rule definition. In the rules text files this will function as a global variable that all the rules can access and that has a lifetime beyond that of any individual rule in that file. You cannot really do this with the UI rules in the same fashion.

You may find that the expanded blockly scripting language may work out well for you here (create the script action in the UI dialog and select blobkly as the language). Blockly now has a way to store values between runs of a rule and the Blockly documentation should go through how to use it.

Don*t do that. Use a second Item to control the “real” Item.
Let’s say MySwitch and MyLight, where MyLight is the “real” Item
Set up an expiration Timer for MyLight (Metadata Expiration Timer-> Send Command OFF after one Second)
Build a rule to toggle the MySwitch (postUpdate, not sendCommand), whenever MyLight received command ON.
Build another Rule which sends a command ON to MyLight, whenever MySwitch received a command.

Rules DSL:

rule "update status of MySwitch"
when
    Item MyLight received command ON
then
    MySwitch.postUpdate(if(MySwitch.state != ON) ON else OFF)
end

rule "send command to MyLight"
when
    Item MySwitch received command
then
    MyLight.sendCommand(ON)
end

Maybe you don’t need the first rule at all. Be aware that there might be a problem to keep both MySwitch and the Light synchronous.

this was what helped me :smiley: i didn’t found how create the rules :slight_smile: now i made a file and pasted this


var lastCom = now
rule "Luce Studio"
when
   Item Luce changed
then
if (lastCom.isBefore(now.minusSeconds(2)) && Luce.state == OFF) {
  Thread::sleep(100)
  Luce.sendCommand(ON)
  Thread::sleep(100)
  Luce.sendCommand(OFF)
  lastCom = now
}
end

it works, when i press the button or when i try to use alexa
for the solution of @Udo_Hartmann maybe it will works too… but i’m working with bticino and i don’t know how integrate that system on the button and i’m 90% sure that the button would brake the sync