Examples of Scenes


(Michel Feldman) #1

I´m new on habpanel, and i wanna to create a widget to make a scene.
For example: click the button and turn off a lamp, turn on another lamp and dimmer another one.
How can in do it on a same button?
Thanks


(George Erhan) #2

Hi Michel,
You can use rules or Group Items (be aware that the status of Groups is “real” as long as all items in group have the same status).
As a an example use something like this below:

In items file

Group SwitchLightGroup1
Dimmer Light_dimmer1 "Dimmed Light" { binding="with the necessary dimming configuration" }
Switch SWLight_dimmer1 "Switch Dimmed Light" (SwitchLightGroup1) { binding="" with the necessary switching configuration - if it exists}
Switch Light_switch2 "Light 2" (SwitchLightGroup1) { binding="with the necessary switching configuration"}
Switch Light_switch3 "Light 3" (SwitchLightGroup1) { binding="with the necessary switching configuration"}

In sitemap file:

Switch item=SwitchLightGroup1 label="Scene" icon="whatever_icon_you_wish"

This is the easy way described above.
The harder way is using rules such as:
In the rules file:

rule Scene1
when Item SwitchLightGroup1 received command ON
then
SWLight_dimmer1.sendCommand(ON)
Light_switch2.sendCommand(ON)
Light_switch3.sendCommand(ON)
end

The difference is that with the Group method you have ON and OFF and with the rule (as it is right now) you only switch the lights ON. Of course you can add mapping to the switch item in sitemaps file such as

Switch item=SwitchLightGroup1 label="Scene" icon="whatever_icon_you_wish" mappings=[ON="Activate"]

Best regards,

George


Scene Alexa Integration
(Michel Feldman) #3

Many thanks George.
Thats what i looking for.
Cheers from Brazil.


(George Erhan) #4

My pleasure,
Take care with the dimmer switching configuration - it very much depends on the binding type (if the dimmer has ON/OFF object, most of them have). You can use rules to send values if the values are of different types.

Cheers from Romania :wink:


(Udo Hartmann) #5

If you want to use more different scenes, you could even use a virtual number item:
.items:

Number myScenes "Scene Number is [%s]" 

.sitemap:

Switch item=myScenes mappings=[0="OFF",1="ON",2="left",3="right"]

.rules:

rule "my scenes switch"
when
    Item myScenes changed
then
    switch (myScenes.state) {
        case 0: {
            Item1.sendCommand(OFF)
            Item2.sendCommand(OFF)
            Item3.sendCommand(OFF)
            Item4.sendCommand(OFF)
        }
        case 1: {
            Item1.sendCommand(ON)
            Item2.sendCommand(ON)
            Item3.sendCommand(ON)
            Item4.sendCommand(ON)
        }
        case 2: {
            Item1.sendCommand(OFF)
            Item2.sendCommand(OFF)
            Item3.sendCommand(ON)
            Item4.sendCommand(ON)
        }
        case 3: {
            Item1.sendCommand(ON)
            Item2.sendCommand(ON)
            Item3.sendCommand(OFF)
            Item4.sendCommand(OFF)
        }
        default: { 
            logInfo("myScenes","incorrect state: {}",myScenes.state)
        }
    }
end

One item (with 4 buttons) and 4 different scenes…


Lights scene setter
Scenes not working
(Max G) #6

Thanks… didn’t know there was a ‘switch’ (case)… :slight_smile:


(Rich Koshak) #7

Beware that unlike most programming languages the cases do not flow through.

For example, in Java I could write:

switch(foo){
    case 1: {
        // do case 1 stuff
    }
    case 2: {
        // do case 1 and 2 stuff
        break;
    }
    case 3: {
        // do case 3 stuff
        break;
    }
    default: {
        // do default stuff
    }
}

In the above if foo is 1 it will execute both the code blocks for case 1 and case 2.

In the rules DSL you can’t do that (and if you try the break will exit the rule). This will probably only be a problem for programmers who are used to switch cases in other languages.


(Jonathan) #8

If you’re open to using jsr223, I’ve just shared a general purpose scene rule that lets you easily define as many scenes and states as you want in an array structure. You can set each item using a specific command (“ON”, “45”, “OFF”, etc…) or with an setting item name which will grab the state of that setting item and apply it to the item you are updating.

The jsr223 rule is in the post here.

Unfortunately jsr223 does not seem to be working in openHAB2 yet, but the rule works great in OH1.


(Avner) #9

This thread has been idle for a while. Do you know if jsr223 is supported in openHAB2 now?
Otherwise, what is the closest scripting language that the jsr223 rule be ported to, such that it could be run in openHAB2 ? (I’m versed with e.g. python, perl, javascript and shell scripting but not with JVM scripting languages)


(Ted Drain) #10

FYI - JSR223 for OpenHab2 is almost here: https://github.com/eclipse/smarthome/pull/1783


(Tom Araya) #11

Is it useable right now in OH2?


(Rich Koshak) #12

As far as I can tell it is merged into the ESH baseline. I do not know how frequently ESH gets merged with OH baseline. There are some real differences, not the least of which is the move to karaf, so it does take some work.


(Samir Ahluwalia) #13

Will this work with Sonoff Switches with Tasmota Firmware


(Rich Koshak) #14

Rules operate on Items. Items are independent of the specific technology or API they are connected to. The above should work for ALL bindings, including Sonoff.


(Samir Ahluwalia) #15

I tried but unfortunately they dont seem to work


(Rich Koshak) #16

then your problem has nothing to do with the above. You either have misconfigured OH to connect to MQTT, misconfigured your Items to subscribe/publish to the right topics, or something like that.

Since you have written a total of two sentences about your problem, only one of which actually says you have a problem, I don’t know what you expect us to go on to help.

I recommend starting a new thread and post details about what you have done, your configs, items, rules, and what your problem is.


(Dan) #17

When I add this code verbatim to my openhab2 install I can’t get this to work. I always get the “incorrect state” message. I even tried to write this as an if/elseif thinking maybe there was something funky with the switch case but I get the same issue. Has something changed in with openhab2 that prevents this code from working? It’s almost as if the numerical 0 from the sitemap isn’t recognized as a 0 in the rules which means the default case is always triggered.


(Dan) #18

I can now answer my own question: In the rules file
switch (myScenes) {

should be

switch (myScenes.state) {


(Josar) #19

@Udo_Hartmann would you be so kind and correct your post? As it raises questions for people which try to reproduce it!


 switch (myScenes.state) {

Thank you in advance.


(Udo Hartmann) #20

Sure. Done :slight_smile: