Create

Hi
I am beginner in Openhab.
I want to create scene with mqtt nodemcu as switch ,whenever we push the manual button scene activate or deactivate.
I have two more mqtt nodemcu device which is connected with led.
I want to those device activate or deactivate by pressing a switch but i dont have idea how to create scene,item,sitemap,rules etc.
please
help
Thanx

My item file:
Switch lamp1 “Office Lamp1” (all){mqtt=">[mosquitto:/home/1/esp01/p1/com:command:on:ON],>[mosquitto:/home/1/esp01/p1/com:command:off:OFF],<[mosquitto:/home/1/esp01/p1/state:state:default]"}

Switch lamp2 “Office Lamp2” (all){mqtt=">[mosquitto:/home/2/esp02/p1/com:command:on:ON],>[mosquitto:/home/2/esp02/p1/com:command:off:OFF],<[mosquitto:/home/2/esp02/p1/state:state:default]"}

Switch button “scene” (all){mqtt=">[mosquitto:/home/3/esp03/p1/com:command:on:ON],>[mosquitto:/home/3/esp03/p1/com:command:off:OFF],<[mosquitto:/home/3/esp03/p1/state:state:default]"}

My sitemap file:
sitemap demo label=“Main Menu”

{

Frame label=“MQTT” {

Switch item=lamp1 label=“Office Lamp1”

Switch item=lamp2 label=“Office Lamp2”

Switch item=button label=“scene”
}

}

This isn’ too bad but in the future please use code fences:

```
code goes here
```

Create a Design Pattern: Proxy Item to represent the scene. You will want to use a Number or a String Item.

Create a Rule triggered by the Proxy Item receiving a command.

In the Rule check the state of the Proxy Item and turn ON or OFF the lights based on the scene selected.

Put the Proxy Item on your Sitemap with a Selection or as a Switch using MAPPINGS.

Please give me a example based on my code .
Thanx

Item files:

Switch lamp1 “Office Lamp1” (all){mqtt=">[mosquitto:/home/1/esp01/p1/com:command:on:ON],>[mosquitto:/home/1/esp01/p1/com:command:off:OFF],<[mosquitto:/home/1/esp01/p1/state:state:default]"}

Switch lamp2 “Office Lamp2” (all){mqtt=">[mosquitto:/home/2/esp02/p1/com:command:on:ON],>[mosquitto:/home/2/esp02/p1/com:command:off:OFF],<[mosquitto:/home/2/esp02/p1/state:state:default]"}

Switch button “scene” (all){mqtt=">[mosquitto:/home/3/esp03/p1/com:command:on:ON],>[mosquitto:/home/3/esp03/p1/com:command:off:OFF],<[mosquitto:/home/3/esp03/p1/state:state:default]"}

Sitemap file

sitemap demo label=“Main Menu”

{

Frame label=“MQTT” {

Switch item=lamp1 label=“Office Lamp1”

Switch item=lamp2 label=“Office Lamp2”

Switch item=button label=“scene”
}
}

First, please change all smart quotes to normal quotes as openHAB does not accept smart quotes at all (but this error may have been caused by copy and paste to the forum)

In question of scenes: an easy way would be a rule, triggered by the scene switch:

rule "my scenes"
when
    Item button received command
then
    if (receivedCommand==ON) {
        lamp1.sendCommand(ON)
        lamp2.sendCommand(ON)
    }
    else if (receivedCommand==OFF) {
        lamp1.sendCommand(OFF)
        lamp2.sendCommand(OFF)
    }
end

If the button does only send ON commands, you could use another rule:

var int counter = 0

rule "my scenes"
when
    Item button received command
then
    if (receivedCommand==ON) {
        counter = counter + 1
        if (counter > 3)
            counter = 0
    }
    switch counter {
        case 0: {
            lamp1.sendCommand(OFF)
            lamp2.sendCommand(OFF)
       }
        case 1: {
            lamp1.sendCommand(ON)
            lamp2.sendCommand(OFF)
       }
        case 2: {
            lamp1.sendCommand(ON)
            lamp2.sendCommand(ON)
       }
        case 3: {
            lamp1.sendCommand(OFF)
            lamp2.sendCommand(ON)
       }
    }
end

This would result in a scene button which would switch through 4 different scenes. In fact, this would be more like a serial switch (don’t know if this is the correct english expression, for details see https://de.wikipedia.org/wiki/Serienschaltung, no english wiki page, but good schematics - “Seriendrehschalter”). When using more lights, one would not increase steps but define only a few scenes with the preffered lighting.

Thanks it’s working but still i have problem.
Openhab item button working well and switch on off another two devices as i want in my scene but manual mqtt push buttons which is connected with nodemcu not working. Whenever we pushed the button scene not activated.may be mistakes in my item or rule
please help

Please post your Items and Rules as they are right now. We don’t know what if any changes you made to them in the past two days.

there is scene “Bedroom Light” is a manual button or push button which is connected with mqtt nodemcu
and i want to operate "Room Light and “kitchen Light” by this scene button ON-OFF manually buttton and by openhab GUI.

my item file

Switch scene "Bedroom Light" [ "Switchable" ] {mqtt=">[mosquitto:/home/3/esp03/p1/com:command:on:ON],>[mosquitto:/home/3/esp03/p1/com:command:off:OFF],<[mosquitto:/home/3/esp03/p1/state:state:default]"}
Switch home1 "Room Light" [ "Switchable" ] {mqtt=">[mosquitto:/home/1/esp01/p1/com:command:on:ON],>[mosquitto:/home/1/esp01/p1/com:command:off:OFF],<[mosquitto:/home/1/esp01/p1/state:state:default]"}
Switch home2 "Kitchen Light" [ "Switchable" ] {mqtt=">[mosquitto:/home/2/esp02/p1/com:command:on:ON],>[mosquitto:/home/2/esp02/p1/com:command:off:OFF],<[mosquitto:/home/2/esp02/p1/state:state:default]"}

and

Sitemap file :

sitemap demo label="Main Menu"
{
   Frame label="HOME AUTOMATION" {
   Switch item=home1 label="Room Light" icon="light"
   Switch item=home2 label="Kitchen Light" icon="light" 
   Switch item=scene label="Bedroom Light" icon="light"
 }  
}

Rules file:

rule "my scenes"
when
    Item scene received command
then
    if (receivedCommand==ON) {
        home1.sendCommand(ON)
        home2.sendCommand(ON)
    }
    else if (receivedCommand==OFF) {
        home1.sendCommand(OFF)
        home2.sendCommand(OFF)
    }
end

and

Transform file onoff.map

1=ON
0=OFF
ON=1
OFF=0

OK, from looking at your rule and looking at your question I’m not sure you understand how this is working.

The scene will only change if you send a command to the scene Item. Pressing a physical button on one of the lights does not send a command to the scene Item.

If you want to have the scene change when you toggle a light from the physical switch, you will need to write rules that trigger when the light changes and send a command to the scene as appropriate.

thanx
please give me a example for rules making.

I can’t write this for you. You’ve already have all the examples you need in the code you’ve already written. It is the exact same concept.

basically i want that the two other mqtt device like “Room Light” and “Kitchen Light” ON and OFF by scene “Bedroom Light” mqtt push Button.

I don’t want to come across as not helpful, but I’ve been down this path before and it ends up being nothing but a time sync. You won’t get what you want and I will have wasted time that I could have spent helping many other users.

We have provided everything in this thread that you should need in terms of examples to implement this yourself. If you cannot then you need to study the Rules and Items you have already working and understand how they work. Once you do it will be trivial to implement this yourself.

I will be happy to answer any specific questions about the examples you may have, but I will not write this for you.

in my item file all mqtt item state is set to default this is correct?

state:state:default

the first “state” in “state:state:default” is the last part of the topic your device is publishing to.

The second “state” means that the messages you read on the topic “/home/2/esp02/p1/state” will cause an update to the Item, not a command.

The “default” means you do not want the MQTT binding to make any changes to the incoming message before attempting to assign it to the Item.

So, if the device publishes “ON” to “/home/2/esp02/p1/state”, the binding will do a postUpdate(ON) to the state. So you will want to trigger an Item using received update or changed for any Rule that you want to execute when the Item is toggled from the device.

Ok Thanks Rich Koshak and sorry for any trouble becoz i’m newbie in openhab again thanx for help!!!

No problem and I understand you are a newbe and I want to help. But in the end it doesn’t help either of us if I just do it for you.

Whenever power goes of my raspberry pi and mqtt devices,openhab restart and change its state as default all light OFF but i want that whenever power goes OFF and again ON that the device state remain same as previous. Please help!!! How can i solve it?

http://docs.openhab.org/configuration/persistence.html