Button for: ON / OFF / SCHEDULE with logic/rules

If have a question on customized buttons in Habpanel or Classic UI. Under Fibaro where I am coming from, I created a virtual device with three buttons for controlling the garden light. I have three options: ON / OFF / SCHEDULE. For selecting “Schedule” a global value will be set as “schedule”. And a script is checking if global value for garden light is equal “schedule” than light (fibaro wall plug) will be switched on when sun goes down, and light will switch off at 10:00 pm.
Any idea how I can build this with openhab2?
Thanks and regards.

1 Like

Create a Number or String Item to represent the Switch state. Then use mappings with a Switch element on the sitemap. Finally a rule that triggers when the String Item receives a command.

Item:

String GardenLight "Garden Light"

Sitemap:

Switch item=GardenLight mappings=[ "On"="ON", "Off"="OFF", "Schedule"="SCHEDULE" ]

Rule:

rule "Garden Light"
when
    Item GardenLight received command
then
    switch GardenLight.state:
        case "ON": {
            // do on
        }
        case "OFF": {
            // do off
        }
        case "SCHEDULE": {
            // do scheudle
        }
end

Just a quick question in case one might only want to fire a rule in case of certain string item changes.
Like only do something in case GardenLight chanes from ON to SCHEDULE. I would have expected some like the following rule trigger would work in OH2

when
  Item GardenLicht changed from "ON" to "SCHEDULE"
then
....

I had tough luck in getting this done. Is this not supposed to work?
Thanks much

Hello Rich,

I’m a beginner with OpenHAB. In the last couple of weeks I’m following the great tutorials, wikis and guides to automate my home. Right now, I’m trying to follow yours instructions above, to get a 3-state button (ON, OFF, Schedule ) but this is not working when I pick the option Schedule.

Items:

String arvore_light "Árvore de Natal" <light> (LR,gLight)
    { mqtt=">[broker:cmnd/sonoff-7825/POWER:command:*:default],
            <[broker:stat/sonoff-7825/POWER:state:default]" }

// A switch being 'ON' as long as the device is reachable
String arvore_light_Reachable "Árvore de Natal: reachable" (gReachable)
    { mqtt="<[broker:tele/sonoff-7825/LWT:state:MAP(reachable.map)]" }

Map:

Online=ON
Offline=OFF
schedule=SCHEDULE

Rules:

rule "Christmas Tree"
when
    Item Christmas_Tree received command
then
    switch LivingRoom_Light.state {
        case "ON": {
            LivingRoom_Light.sendCommand(ON)
        }
        case "OFF": {
            LivingRoom_Light.sendCommand(OFF)
        }
        case "SCHEDULE": {
      
            LivingRoom_Light.sendCommand(ON) // Should be a cron condition, but for testing I just want to turn it on
        }
        
   }
end

Site Map:

Switch item=arvore_light mappings=[ "On"="ON", "Off"="OFF", "Schedule"="SCHEDULE" ]

Event log:

2017-11-12 16:29:34.943 [ItemStateChangedEvent     ] - arvore_light changed from Off to Schedule

Can you help me to figure that out ?

switch(LivingRoom_Light.state.toString) {