Help converting OH2 Rule to OH3

OH3.1 Milestone on RPi4

Can someone please help me convert my Kitchen Motion rule to OH3? I’m having a very difficult time with complex rules in the UI that used the Expire function:

Items:

	Dimmer Sink1 				"Sink1" 				<hue> 		(KitchenLight, Kitchen_Group, Lights, StartPersist)			{channel="hue:0100:00178860ae33:21:brightness"}
	Dimmer Sink2 				"Sink2" 				<hue> 		(KitchenLight, Kitchen_Group, Lights, StartPersist)			{channel="hue:0100:00178860ae33:19:brightness"}
	Dimmer Stove1 				"Stove1" 				<hue> 		(KitchenLight, Kitchen_Group, Lights, StartPersist)			{channel="hue:0220:00178860ae33:25:brightness"}
	Dimmer Stove2 				"Stove2" 				<hue> 		(KitchenLight, Kitchen_Group, Lights, StartPersist)			{channel="hue:0220:00178860ae33:28:brightness"}
	Color Fridge1 				"Fridge1" 				<hue> 		(KitchenLight, Kitchen_Group, Lights, StartPersist)			{channel="hue:0210:00178860ae33:27:color"}
	Color Fridge2 				"Fridge2" 				<hue> 		(KitchenLight, Kitchen_Group, Lights, StartPersist)			{channel="hue:0210:00178860ae33:26:color"}

String   KitchenArmedTripped "Kitchen Sensor Armed/Tripped [%d]" 					(Kitchen_Group) {mios="unit:house,device:208/service/SecuritySensor1/ArmedTripped"}
Switch Kitchen_TIMER_5m_OFF 							(Virtuals, StartPersist)	{ expire="6m,command=OFF" }
Switch Kitchen_TIMER_61s_OFF 							(Virtuals, StartPersist)	{ expire="65s,command=OFF" }

Rules:

rule "Kitchen motion sensor fires"
    when
        Item KitchenArmedTripped changed to 1

    then 
		val HSBType cRed    = new HSBType(new DecimalType(0),new PercentType(100),new PercentType(100))
		val HSBType cWhite  = new HSBType(new DecimalType(0),new PercentType(0),new PercentType(100))

		logInfo("Kitchen Motion", "Lux Reading, lights on KitchenLux.state =" + KitchenLux.state.toString)

        if (KarenBedtime.state == OFF){
			logInfo("Day time Kitchen Motion", "KitchenArmedTripped.state = " + KitchenArmedTripped.state.toString)
			sendCommand(KitchenLight, ON)
			sendCommand(Fridge1, cWhite)
			sendCommand(Fridge2, cWhite)

		} else {
			logInfo("Night time Kitchen Motion", "KitchenArmedTripped.state = " + KitchenArmedTripped.state.toString)
			sendCommand(Fridge1, cRed)
			sendCommand(Fridge2, cRed)
		}
		
        Kitchen_TIMER_5m_OFF.sendCommand(ON)
		Kitchen_TIMER_61s_OFF.sendCommand(ON)

end

rule "Kitchen_TIMER_5m_OFF changed to  OFF"
    when
        Item Kitchen_TIMER_5m_OFF changed to OFF
    then
		if (KitchenArmedTripped.state == "1") {
			logInfo("Kitchen Motion", "Kitchen Motion still detected, reset timer.")
			Kitchen_TIMER_61s_OFF.sendCommand(ON)
			Kitchen_TIMER_5m_OFF.sendCommand(ON)
		} else {
		logInfo("Kitchen Motion", "KitchenArmedTripped.state=" + KitchenArmedTripped.state)
        logInfo("Kitchen Motion", "No Motion Detected.  Turning off kitchen")
        sendCommand(KitchenLight, OFF)
		}		
end


rule "Kitchen_TIMER_61s_OFF changed to OFF"
    when
        Item Kitchen_TIMER_61s_OFF changed to OFF
    then
		if (KitchenArmedTripped.state == "1") {
			logInfo("Kitchen Motion", "Kitchen Motion still detected, reset timer.")
			Kitchen_TIMER_61s_OFF.sendCommand(ON)
			Kitchen_TIMER_5m_OFF.sendCommand(ON)
		} else {
			logInfo("Kitchen Motion", "60s timer off KitchenArmedTripped.state = " + KitchenArmedTripped.state)	
		}
end

Perhaps you could tell us what you expected to happen, and when, and what happened instead? What does your openhab.log and events.log tell you about what is going on?

I don’t know how to code this in OH3, so nothing is happening. The items and code snips are from OH2.

In OH2, when the kitchen motion sensor fired, it would turn on the kitchen lights. It used two different timers, a 5 minute and 60 second to detect if there was still motion in the kitchen. It it was “after bedtime”, it would turn the kitchen lights red, it it was daytime, the lights turn on normally.

You can use xxx.rules files and xxx.items files directly in OH3.

OH that is AWESOME, you just made my day!! I read all the tutorials yesterday and didn’t see that was a possibility!

Thank you!!

Note that your KitchenArmedTripped Item will not work as it is. It was linked to a version 1.x binding, no longer available in OH3.
Your next adventure will be finding out about Things and channels.

Note also that there are a few changes affecting rule code, notably datetime handling.

That is a zwave device and I was on the latest OH2 version before being dragged to OH3.

Sounds like I can’t just drop my old rules and have them work in OH3, so I’m back to how do I get the rules posted to OH3.

BTW, OH2 has Things and Channels, so I’m not sure why you think that is different in OH3.

No-one has dragged you anywhere. Maybe you allowed automatic updates at some time, or you chose OH3 for some feature you wanted, I don’t know.
OH2 continues to be available if you don’t want to use OH3.

This is still true.
If you have file based Items linked to v1.x bindings, like this one is linked to mios-

you will have to do something about it.
I assumed there would be a Mios things & channels binding version, but I was mistaken.
People have developed an HTTP approach instead here -

You do not have to throw out the baby with the bathwater, unless you choose to do so.
If you have old OH2 rules using datetime processing, you will need to update those because it is handled differently in OH3.
If you have old OH2 rules using executeCommandLine, you will need to update those because it is handled differently in OH3.

Many of the bindings introduced Quantity Type channels (Units of Measurement) alongside OH3 updates, so you may find some of your Items now have units.
Particularly in the case of comparisons, some old OH2 rules may need updating to handle units.

These changes would be needed whether your rules are files based or GUI entered, that part makes no difference.
There’s 9 months of people gone before you in this, so lots of threads already with advice to overcome stumbling blocks.

If you want to enter your rules in GUI, do so. You can convert piecemeal if you like, files and GUI rules can co-exist.

Like most GUI things, try it, click and do. Just like in files, you choose some event to trigger your rule, usually an Item event. The GUI also offers conditions for triggering, like say some other Item state, For very simple rule actions like sending some other Item a command, this can beentered directly in the GUI. For the average rule with its ifs-and-buts, you’ll want a scripted section - corresponding to the rule body between ‘then’ and ‘end’ in a file rule. You can choose to script in various languages - including familiar DSL.

1 Like

Rossko
this should be part of a how to convert my OH2 rules to OH3 tutorial
:+1:

Yes! With lots of examples!

I’m slowly converting each of the OH2 rules and now I’m getting errors here and don’t know how to use OH3 rule:

This rule that sets the light’s color based on if I’ve gone to bed:

rule "Kitchen motion sensor fires"
    when
        Item KitchenMotion_MotionAlarm changed to ON

    then 
		val HSBType cRed    = new HSBType(new DecimalType(0),new PercentType(100),new PercentType(100))
		val HSBType cWhite  = new HSBType(new DecimalType(0),new PercentType(0),new PercentType(100))

		logInfo("Kitchen Motion", "Lux Reading, lights on KitchenMotion_Sensorluminance.state =" + KitchenMotion_Sensorluminance.state.toString)
		logInfo("Bedtime", "Bedtime State, KarenBedtime.state =" + vKarenBedtime.state.toString)
        if (vKarenBedtime.state == OFF){
			logInfo("Day time Kitchen Motion", "KitchenMotion_MotionAlarm.state = " + KitchenMotion_MotionAlarm.state.toString)
			sendCommand(KitchenLight, ON)
			sendCommand(Fridge1, cWhite)
			sendCommand(Fridge2, cWhite)

		} else {
			logInfo("Night time Kitchen Motion", "KitchenMotion_MotionAlarm.state = " + KitchenMotion_MotionAlarm.state.toString)
			sendCommand(Fridge1, cRed)
			sendCommand(Fridge2, cRed)
		}
		
        Kitchen_TIMER_5m_OFF.sendCommand(ON)
		Kitchen_TIMER_61s_OFF.sendCommand(ON)

end

is getting this error in the log:

2021-09-08 12:58:51.499 [WARN ] [org.openhab.core.items.GroupItem    ] - Command '0,100,100' has been ignored for group 'Fridge1' as it is not accepted.
2021-09-08 12:58:51.503 [WARN ] [org.openhab.core.items.GroupItem    ] - Command '0,100,100' has been ignored for group 'Fridge2' as it is not accepted.

How can I use sendCommand to set specific colors for a bulb in OH3?

Well, your Item is a Group type, not a bulb. What sub-type is your group, a Group:Color perhaps? With Color type members?

The Group type isn’t the one trying to set the color. The error is on Fridge 1 and 2 which are colored bulbs and not part of a group:

Variable defined:

val HSBType cRed    = new HSBType(new DecimalType(0),new PercentType(100),new PercentType(100))

Commands:

sendCommand(Fridge1, cRed)
sendCommand(Fridge2, cRed)

May we see your Item definitions for Fridge1 and Fridge2?
This isn’t idle curiosity, it’s based off the info that you already usefully shared

1 Like

Please try to use the method instead of the action. This is, do not use sendCommand(Fridge1, cRed) but Fridge1.sendCommand(cRed)

A slightly changed rule:

rule "Kitchen motion sensor fires"
when
    Item KitchenMotion_MotionAlarm changed to ON
then
    val HSBType cRed   = new HSBType(new DecimalType(0),new PercentType(100),new PercentType(100))
    val HSBType cWhite = new HSBType(new DecimalType(0),new PercentType(0),new PercentType(100))

    logInfo("kitchenMotion", "Lux Reading, lights on KitchenMotion_Sensorluminance.state = {}", KitchenMotion_Sensorluminance.state)
    logInfo("kitchenMotion", "Bedtime State, KarenBedtime.state = {}", vKarenBedtime.state)
    var theColor = cRed
    var strDaytime = "Night"
    if(vKarenBedtime.state == OFF){
        KitchenLight.sendCommand(ON)
        theColor = cWhite
        strDaytime = "Day"
    }
    Fridge1.sendCommand(theColor) // Fridge1 is a Color Item?
    Fridge2.sendCommand(theColor) // Fridge2 is a Color Item?
    logInfo("kitchenMotion", "KitchenMotion_MotionAlarm.state = {} and it's {}.", KitchenMotion_MotionAlarm.state, strDaytime)
    Kitchen_TIMER_5m_OFF.sendCommand(ON)
    Kitchen_TIMER_61s_OFF.sendCommand(ON)
end
1 Like

Thank you for your help. What are the advantages of using Fridge1.sendCommand(cRed) vs. sendCommand(Fridge1, cRed)?

Thank you!! Your comment really confused me, but showing me the error line pointed me to what was happening. I was able to get the code running correctly! Six rules down, only 148 to go! Ugh.

Would you share your fix, for future readers benefit? :grinning:

Virtual timer items did not change, motion sensor moved to zwave stick and added to UI.

rule "Kitchen motion sensor fires"
    when
        Item KitchenMotion_MotionAlarm changed to ON

    then 
		val HSBType cRed    = new HSBType(new DecimalType(0),new PercentType(100),new PercentType(100))
		val HSBType cWhite  = new HSBType(new DecimalType(0),new PercentType(0),new PercentType(100))

		logInfo("Kitchen Motion", "Lux Reading, lights on KitchenMotion_Sensorluminance.state =" + KitchenMotion_Sensorluminance.state.toString)
		logInfo("Bedtime", "Bedtime State, KarenBedtime.state =" + KarenBedtime.state.toString)
        if (KarenBedtime.state == ON){
			logInfo("Day time Kitchen Motion", "KitchenMotion_MotionAlarm.state = " + KitchenMotion_MotionAlarm.state.toString)
				
			Fridge1_Color.sendCommand(cRed)
			Fridge2_Color.sendCommand(cRed)

		} else {
			logInfo("Night time Kitchen Motion", "KitchenMotion_MotionAlarm.state = " + KitchenMotion_MotionAlarm.state.toString)
			Fridge1_Color.sendCommand(cWhite)
			Fridge2_Color.sendCommand(cWhite)
			Stove1.sendCommand(ON)
			Stove2.sendCommand(ON)
			Sink1.sendCommand(ON)
			Sink2.sendCommand(ON)
		}
		
        Kitchen_TIMER_5m_OFF.sendCommand(ON)
		Kitchen_TIMER_61s_OFF.sendCommand(ON)

end

rule "Kitchen_TIMER_5m_OFF changed to  OFF"
    when
        Item Kitchen_TIMER_5m_OFF changed to OFF
    then
		if (KitchenMotion_MotionAlarm.state == "1") {
			logInfo("Kitchen Motion", "Kitchen Motion still detected, reset timer.")
			Kitchen_TIMER_61s_OFF.sendCommand(ON)
			Kitchen_TIMER_5m_OFF.sendCommand(ON)
		} else {
		logInfo("Kitchen Motion", "KitchenMotion_MotionAlarm.state=" + KitchenMotion_MotionAlarm.state)
        logInfo("Kitchen Motion", "No Motion Detected.  Turning off kitchen")
			Fridge1_Color.sendCommand(OFF)
			Fridge2_Color.sendCommand(OFF)
			Stove1.sendCommand(OFF)
			Stove2.sendCommand(OFF)
			Sink1.sendCommand(OFF)
			Sink2.sendCommand(OFF)
		}		
end


rule "Kitchen_TIMER_61s_OFF changed to OFF"
    when
        Item Kitchen_TIMER_61s_OFF changed to OFF
    then
		if (KitchenMotion_MotionAlarm.state == "1") {
			logInfo("Kitchen Motion", "Kitchen Motion still detected, reset timer.")
			Kitchen_TIMER_61s_OFF.sendCommand(ON)
			Kitchen_TIMER_5m_OFF.sendCommand(ON)
		} else {
			logInfo("Kitchen Motion", "60s timer off KitchenMotion_MotionAlarm.state = " + KitchenMotion_MotionAlarm.state)	
		}
end