Does openHAB2 support Presets?

Does openHAB2 support the idea of Presets?

A preset is a group of actions.

For example, you may have several light bulbs (LIFX, Philips Hue etc.) around your house. With a single command you activate a preset and the lights are set to a predefined configuration, ie. the kitchen and living room lights turn on at 50% while the bedroom and outdoor lights turn off.

Presets are not limited to lights but can be an action on any Item Type.
Presets can be created/modified and removed.

Presets Vs. Rules

  • Presets are a group of actions. They do not include Rules within them. (eg. Night Preset is Turn Light 1 & 2 ON)
  • Presets can be assigned as the Action of a Rule. Rules contain a trigger, condition and action. (eg. At 8pm activate the Night preset).

Presets can be implemented in rules. But there is as of yet no separate way to do it outside of rules.

There are lots of ways people have implemented them using Rules. The big difference in all the approaches come down to how one stores the values for each Item at each preset:

  • Using groups and similarly named Items which store the values for each Item at each preset
  • Using a hashMap for each preset keyed on the Item name and storing the Item’s desired state for that preset
  • ArrayList and index tracking to match up Items to their preset values

I don’t have dimmers but the overall concept of how I would implement this (and how I have for simple switches) is posted here (Group and Filter Design Pattern):

Also see the related Time of Day design pattern in that same thread.

So talking about presets I’ve wanted to ask something:
I have these 3 Buttons:

Number Group_preset (gLight) ->

	when
			Item gLight received command ON
then
		if (Group_preset.state === null)
		{sendCommand(gLight, ON)}
		else{ 
				
		switch (Group_preset.state){
		//all sleepingroomlights
		case 1:
		{
		sendCommand(gLight, OFF)
		sendCommand(Yeelight1_p, ON)
		sendCommand(Yeelight2_p, ON)
		}
		//all livingroomlights
		case 2:
		{
		sendCommand(Yeelight1_p, OFF)
		sendCommand(Yeelight2_p, OFF)
		}
		//all lights
		case 3:
		{
			
		}
	}	
		}
		end
		
		rule "reset preset"
		when 
		Item gLight received command OFF
		then 
Group_preset.state = null
sendCommand(gLight, OFF)
		end 

Goal is to shut down some of the items of the gLight group, if one preset is selected.
But they just go crazy and I don’t know why, the logic should be allright.
Can someone help me?

Are you tryng to update an Item?

Group_preset.postUpdate(0)

I’m not sure that updating to null works, I’d be inclined to avoid it anyway (you might want to tell the difference between an uninitialized Item and a ‘zeroed’ one at some stage)

when 
   Item gLight received command OFF
then 
   sendCommand(gLight, OFF)

This looks like an endless loop. If the Group has received an OFF, why send it another OFF?

Yes, thanks!
But my main Question belongs to this part of the Code:

when
			Item gLight received command ON
then
		if (Group_preset.state === null) // or Group_preset.postUpdate(0)
		{sendCommand(gLight, ON)}
		else{ 
				
		switch (Group_preset.state){
		//all sleepingroomlights
		case 1:
		{
		sendCommand(gLight, OFF)
		sendCommand(Yeelight1_p, ON)
		sendCommand(Yeelight2_p, ON)
		}
		//all livingroomlights
		case 2:
		{
		sendCommand(Yeelight1_p, OFF)
		sendCommand(Yeelight2_p, OFF)
		}
		//all lights
		case 3:
		{
			
		}
	}	
		}
		end

Why doesn’t this work?

  1. Switch/Case
    Another thread states that getting into trouble with switch/case and numbers.
    (Problem with case and switch in rules)

The solution was

Switch((Group_preset.state as DecimalType).intValue)
{
   case 1: {
           }
   case 2: {
           }
}

The problem is that the number item can be a float value and “1.0” is not the same as “1” for switch/case…

  1. Group_preset
    As far as I know, you can’t set an item to null. But if you use a number item, you can set it to Zero (0) which is different from null or uninitialized.
    I recommend use Zero (=0) instead of null.
    => as @rossko57 mentioned:
        Group_preset.postUpdate(0) 

In your rule you can remove the if/else and just go with switch/case

Switch((Group_preset.state as DecimalType).intValue)
{
   case 0: {
             gLight.sendCommand(ON)
           }
   case 1: {
             gLight.sendCommand(OFF)
             Yeelight1_p.sendCommand(ON)
             Yeelight2_p.sendCommand(ON)
           }
   case 2: {
           }
}

  1. sendCommand
    It’s better to use the method of an item instead of the action. So use gLight.sendCommand(OFF) or Yeelight1_p.sendCommand(ON) instead of sendCommand(Yeelight1_p, ON)

The difference:
the method of the item knows exactly of what type the state is. The action has to guess the type of state and could be wrong…

Andreas

Can I put in my little bit here,
Group_preset belongs to the group gLight
In your rules, you are going in circle (But they just go crazy!)
Remove Group_preset from the group

Number Group_preset

And comment out case 3 just in case

	//all lights
		//case 3:
		//{
			
		//}

As as @imhofa mentionned:

Thank you guys a lot!
All works as excepted.
I