Better Scenes with Number/Dimmer Items instead of Switch

At the moment scenes are only possible with switch items for google assistant - for example:

Switch  GHS_Movie       "OH Movie"      ["Scene"]
Switch  GHS_Morning     "OH Morning"    ["Scene"]
Switch  GHS_Music       "OH Music"      ["Scene"]

It would be great being able to use Dimmer items for scenes in combination with google assistant - similar to the way fan speeds can be defined. This would save a lot of helper items that are required with the current switch limitation.

Dimmer { ga="Scene" [ scenes="0=movie:film,1=morning,breakfast,2=music,spotify",100=default,standard,unknown, lang="en", ordered=true ] }`

How do you like this idea or does anybody have better ways for using scenes with google assistant and OpenHAB?

Hey,

this will very likely not work, since a scene at Google is actually a switch that only can be turned on.
Thus, your proposed configuration would theoretical mean the creation of 6 separate scenes in your assistant using one item. (Which also would be a completely new paradigm, since currently every item at maximum creates one device at Google.)

Additionally, I currently not really get how the dimmer solution would save you “a lot of helper items” in comparison of the switches. Maybe you can share more insights here to find a nicer solution.

I am using scene numbers for my scenes.

Dimmer  Scenes_Multimedia  label="Multimedia Scene"  { ga="Scene" [ scenes="0=off:power off,1=music:spotify,2=film:chromecast:youtube", lang="en", ordered=true ] }`

For example Scenes_Multimedia in my sitemap looks like this:

Selection  item=Scenes_Multimedia  label="Multimedia [ ]"  mappings=[0='OFF', 1='Music', 2='Film']

A scene selection sets the scene number of the item Scenes_Multimedia to the corresponding scene number. The rule is triggered by the Scenes_Multimedia selection and executes the commands for the selected scene number:

rule "Set multimedia scene"
	when
		Item Scenes_Multimedia received command
	then
		switch receivedCommand as Number {
			case 0: {
				// power off every multimedia device
			}
			case 1: {
				// play music in all rooms
			}
			case 2: {
				// play youtube on chromecast
			}
		}
end

If google assistant would map the scene configuration internally to On/Off items this would reduce lots of helper switch items and code to set scene numbers when a switch gets active.

Am i wrong with my way of handling scenes in openhab?

There’s nothing wrong with what you’re doing for scenes, it just doesn’t line up with how GA works. You could use the fan type if you really want that, but I would also think it’s better to just add the proxy items you need, with a single rule to translate them into the appropriate numbers. I wouldn’t see that as being a lot of additional items/code.

BTW, I like how you use the dimmer item to assign numbers to your scene. I do something similar, but I just use a string item to receive the values.

Thanx for your feedback. There are three reasons why i like Dimmer or Number for scenes.

  1. Selection in sitemap only allows numbers or ON/OFF as value
  2. Numbers in the database are better than strings
  3. Visualization in Grafana is a lot easier with numbers

What i don’t like is that you have to define constants als mappings in every rules file to get better readable rules because global imports or constants for all rules are not possible.

// global definition for scene (enum)
val POWEROFF = 0
val MUSIC = 1
val FILM = 2

In the rule i am using these constants to get better readable code:

rule "Set multimedia scene"
	when
		Item Scenes_Multimedia received command
	then
		switch receivedCommand as Number {
			case POWEROFF: {
				// power off every multimedia device
			}
			case MUSIC: {
				// play music in all rooms
			}
			case FILM: {
				// play youtube on chromecast
			}
		}
end

It would be great to be able to use the transform map scenes.map as kind of enum table.

0=POWEROFF
POWEROFF=0
1=MUSIC
MUSIC=1
2=FILM
FILM=2

To get a mapping in both directions you have to set one mapping with the scene number as key and the scene name as value and a second mapping with key/value swapped. But then you would get duplicate entries in a selection if that would be possible. So what is missing is a transform entry that works in both directions like an enum does.

In the rules file you then woud need something like:

rule "Set multimedia scene"
	when
		Item Scenes_Multimedia received command
	then
		switch receivedCommand.toString {
			case transform("MAP", "scenes.map", "POWEROFF"): {
				// power off every multimedia device
			}
			case transform("MAP", "scenes.map", "MUSIC"): {
				// play music in all rooms
			}
			case transform("MAP", "scenes.map", "FILM"): {
				// play youtube on chromecast
			}
		}
end

This would be some kind of global enum but it is not readable very well in my opinion.
And the other big problem is that there is no way to use this mapping in the selection. So scenes have to be hard coded in the sitemap which is not very nice. Here is a discussion about this missing feature:
Selection with transform.map file as map items

At the moment it is not possible with the transform map.
So adding constants as mapping where needed is the way i go at the moment.

Or do you have any better solution for something like a global enum?

I use selection in my sitemap with strings and it works just fine.

Selection item=HarmonyBedroomActivity mappings=["Bedroom TV"="TV", "Chromecast"="Chromecast", "PowerOff"="Off"]

I don’t know why numbers would be better than strings in the database (I’m not a very experienced programmer), and I don’t use Grafana. So, strings work fine for my purposes.

I know next to nothing about enum, so I’m no help to you there. And it’s Friday night after a long week for me, so I’m having trouble wrapping my head around it. :wink:

Thanx for the info that strings are also possible in Selection items.
I think i will have to live with this limitations and use some duplicate code or helper items.

I use helper items (I call them proxy items or unbound items) a lot as go-betweens, or when I want to convert a value to something that’s more UI-friendly. It is a bit roundabout at times, but it works well enough.

1 Like

For such cases I still use the IFTTT way.
So created a rule in IFTTT that matches a specific sentence in Google Assistant e.g. “Set scene to placeholder” and send the placeholder value to an openHAB item as string.
Works quite well.

But with that you will ofc not have any switches or items in your google home app.

1 Like