Convert Old .rules Rule to OH3

Hi,

How can I turn these 2 rules I used in OH2 to become full main ui rules (so they can be edited through the ui)?

With the first of these rules I am unsure how to integrate them via the main ui because of the use of timers:

Rule 1:

var Timer pfortentimer3s = null

var Timer pfortentimer5s = null

rule "Pfortentimer3s"

when

    Item Pfortenoeffner received command 3

then

    if (pfortentimer3s === null)

    {

        PfortenFfner_1_State.sendCommand(ON)

        pfortentimer3s = createTimer(now.plusSeconds(3)) [|

            pfortentimer3s.cancel()

            pfortentimer3s = null

            PfortenFfner_1_State.sendCommand(OFF)

        ]

    }

end

rule "Pfortentimer5s"

when

    Item Pfortenoeffner received command 5

then

    if (pfortentimer5s === null)

    {

        PfortenFfner_1_State.sendCommand(ON)

        pfortentimer5s = createTimer(now.plusSeconds(5)) [|

            pfortentimer5s.cancel()

            pfortentimer5s = null

            PfortenFfner_1_State.sendCommand(OFF)

        ]

    }

end

With the second one I am unsure on how to tto the switch cases within the main ui and would be grateful for guidance.

Rule 2:

rule "Xiaomi Switch - LivingRoom"
when
    Item XiaomiSwitchLivingRoom_click received update
then
    switch(XiaomiSwitchLivingRoom_click.state.toString) {
        case "single": {
            if(LivingRoomLight.state != 0 && (LivingRoomLight.state > 43  || LivingRoomLight.state < 41)) {
	            LivingRoomLight.sendCommand(OFF)
	        }
	        else {
	            LivingRoomLight.sendCommand(6)
	        }
        }
        case "double": {
        	if(LivingRoomLight.state != 0 && (LivingRoomLight.state > 7 || LivingRoomLight.state < 5)) {
	            LivingRoomLight.sendCommand(OFF)
	        }
            else {
            	LivingRoomLight.sendCommand(42)
	        }            
        }
        case "triple": {
            Pfortenoeffner.sendCommand(3)
        }
        case "quadruple": {
        }
        case "many": {
        }
        case "long": {
            Pfortenoeffner.sendCommand(3)
        }
        case "long_release": {
        }                
    }
end
1 Like

See

What do you think is different?

First rule just use expire metadata and when the Pfortenoeffner item receives an ON command the timer will start automatically.
Then remove the rule you have.

Here is where to add the metadata on the item:

Make sure you set the Do send command to sen an OFF and set you timer to 5 seconds.
image

That is all I use for setting timers.

2 Likes

Note that you cannot use expire timer to do different things depending on command, including ignoring some some commands, or running different times.

But if you just want to turn something off after 5 seconds it should be OK ?

Sure.

But OPs original rules run different times depending on command received, or no time for other commands.

You could replicate that with two dummy timer Items with different times. The rule would do what else it does and choose to trigger one or other timer Item.
Another rule would listen for the timer Items expiring, and then do what was required to the real Item.

The virtual/dummy items with different expire times is what I use. Works well.