(Solved) Using receivedCommand to send a value within sendCommand

  • openHAB version: 2.5
  • Issue of the topic:
    I have lights that can be set to 5 scenes ranging from OFF (0) to All lights at 100% (4). Each scene value of each light is persisted.
Switch Light_VH_Woonkamer_Leeslampje                 // The light itself
    Switch Light_VH_Woonkamer_Leeslampje_S1          // S1 value, there is no S0 as S0 is OFF
    Switch Light_VH_Woonkamer_Leeslampje_S2          // S2 value
    Switch Light_VH_Woonkamer_Leeslampje_S3          // S3 value, there is no S4 as S0 is 100%/ON

I then have a rule that sends the appropriate value when the scene is selected.All the cases are written out full but that results in much code; 5 cases times 10 lights, you get the picture.
So I am thinking to shorten the code by using “receivedCommand” in my sendCommand code:


rule "Woonkamer Scenes"
when
	Item Scene_VH_Woonkamer received command
then
// 0="Uit", 1="TV", 2="Cosy", 3="Lezen", 4="Maximaal"
	switch (receivedCommand){
		case 0:{	
			sendCommand(Light_VH_Woonkamer_Leeslampje, OFF)
    }
        case 1,
        case 2,
        case 3:{ 
		     sendCommand(Light_VH_Woonkamer_Leeslampje, "Light_VH_Woonkamer_Leeslampje_S" + receivedCommand + ".state.toString")
    }
        case 4:{ 
		     sendCommand(Light_VH_Woonkamer_Leeslampje, "ON")
    }
}
end

I was hoping that this would result in using the received command to send the right scene value but in stead I get the following errors:

2020-03-14 16:10:41.871 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert 'Light_VH_Woonkamer_Leeslampje_S1.state.toString' to a command type which item 'Light_VH_Woonkamer_Leeslampje' accepts: [OnOffType, RefreshType].

It looks like the 2nd part of the sendCommand sees a string where I expected it to be “ON” of “OFF”

any help is appreciated

kr

Han

Just by putting together the various command with static strings and + you will inevitably end up with a string.

"Light_VH_Woonkamer_Leeslampje_S" + receivedCommand + ".state.toString"

I’m not quite sure what you want to achieve here for case 3 though. You say “ON” of “OFF”, but under what circumstances for case 3? Don’t you want to piece together the item receiving the command, not the command itself as you are doing now?

something like

sendCommand("Light_VH_Woonkamer_Leeslampje_S" + receivedCommand, ON)

Also you seem to be missing a closing } bracket at the end of the switch case, and you might want to look into the

Light_VH_Woonkamer_Leeslampje.sendCommand(ON/OFF)

notation in general.

What I am trying to archieve is that if the scene command is 1, 2 or 3 that item Light_VH_Woonkamer_Leeslampje is receiving the persisted value (ON of OFF) that is part of that scene. So in scene 1 the light is OFF but in scene 2 its ON.

So I want to piece together on of the 3 possible values and send it to the light based on the receivedCommand.

The code I used is just a small part of the total code, my livingroom has about 15 different lights, be it ON/OFF or Dimmertypes. I now have about 4 pages of sendCommand clauses with case statements to make it work :frowning_face:

3 possible values - since Light_VH_Woonkamer_Leeslampje is defined as a Switch it should only have the two On or OFF possible values (disregarding a 3rd UNDEF :). Also sicne you have 5 different scenes why don’t oyu hard code the ON/OFF value for each lanp within these 5 switch cases.

rule "Woonkamer Scenes"
when
	Item Scene_VH_Woonkamer received command
then
    // 0="Uit", 1="TV", 2="Cosy", 3="Lezen", 4="Maximaal"
    switch (receivedCommand){
        case 0 : {	
            sendCommand(Light_VH_Woonkamer_Leeslampje, OFF)
        }
        case 1 : {	
            sendCommand(Light_VH_Woonkamer_Leeslampje, OFF)
        }
        case 2 : {	
            sendCommand(Light_VH_Woonkamer_Leeslampje, ON)
        }
        case 3:{ 
	    sendCommand(Light_VH_Woonkamer_Leeslampje, ON)
    }
        case 4:{ 
	    sendCommand(Light_VH_Woonkamer_Leeslampje, ON)
    }
}
end

For the dimmers you can define different vlaues being sent for each of the switch cases.

Or is it that you have the 3 different switches

Switch Light_VH_Woonkamer_Leeslampje_S1
Switch Light_VH_Woonkamer_Leeslampje_S2
Switch Light_VH_Woonkamer_Leeslampje_S3

that you can change the ON/OFF state for the different scenes in a different ‘scene settings’ interface?

In that case you should not concatenate the value but try

...
case 2 : {	
           sendCommand(Light_VH_Woonkamer_Leeslampje, Light_VH_Woonkamer_Leeslampje_S2.state)
        }
case 3:{ 
	   sendCommand(Light_VH_Woonkamer_Leeslampje, Light_VH_Woonkamer_Leeslampje_S3.state)
    }
...

if I understood your intention correctly.

If you did not understand my problem then I did not explain it clearly :wink:

I will just concentrate on one light, the rest is merely the same.

I have a light:

Switch Light_VH_Woonkamer_Leeslampje “Leeslampje (WK)” (gLights_VH_Woonkamer)
{mqtt=">[mosquitto:home/esp8266/tasmota/sonoff_BC6459/cmnd/POWER1:command:*:default],
<[mosquitto:home/esp8266/tasmota/sonoff_BC6459/stat/POWER1:state:default]"}

This light can be ON or OFF

I then have 3 different scene settings for this light:


Switch Light_VH_Woonkamer_Leeslampje_S1          
Switch Light_VH_Woonkamer_Leeslampje_S2          
Switch Light_VH_Woonkamer_Leeslampje_S3
       


I set these switches by another piece of code that learns my scene. For this lets presume in scene 1 the light is off , for scene 2 its on and for 3 off again:

```csv
    Switch Light_VH_Woonkamer_Leeslampje_S1       => OFF   
    Switch Light_VH_Woonkamer_Leeslampje_S2       => ON   
    Switch Light_VH_Woonkamer_Leeslampje_S3       => OFF

these values are persisted. So now I send scene “1” the value of Switch Light_VH_Woonkamer_Leeslampje_S1 is being send to the light, which is OFF in this case.

Are you getting the idea?

Yes, this is what I assumed in the last part of my last reply. So if you send the states of the

in each relevant switch cases, without trying to concatenate the S1, S2 and S3 names themselves, as I posted above then it should work.

... 
case 2 : { 
    sendCommand(Light_VH_Woonkamer_Leeslampje, Light_VH_Woonkamer_Leeslampje_S2.state) 
}     
case 3: { 
    sendCommand(Light_VH_Woonkamer_Leeslampje, Light_VH_Woonkamer_Leeslampje_S3.state) 
}
...

Or is there another reason why you might want to concatenate the names as well?

I am trying to reduce the number of lines in my rules files. At the moment I have have 5 case statements of which 3 (1,2 and 3) have 15 lines each. The only thing that differs in the 1,2 & 3 case lines is the “S_”, underscore being 1, 2 or 3. So yes, I want to concatenate the names as well.

Then I’m afraid I’m stuck with getting the string, which you will always end up with the concatination, to an OnOffType from which you need to get the state.

Someone else might be able to point you in the right direction.

All the best

Im not sure im getting what you want to achieve but im just gonna drop my scenes i made for sitemap.
Maybe you can do something with it:

Items:

Number Mijnscenes "Light Scenes" <bulb> 
Number myScenes2 "Master Switches" <bed>

Rules

rule "my scenes switch"
when
    Item Mijnscenes received update
then
    switch (Mijnscenes.state) {
        case 1: {
          sendCommand (Huiskamer_lichten, ON)
		sendCommand (Kitchendimmer, 100)
		sendCommand (Huiskamerdimmer, 0)
		sendCommand (KitchenKleur, 0)
		sendCommand (Keukenled,ON)
		
        }
        case 2: {
           sendCommand (Huiskamer_lichten, ON)
		sendCommand (Kitchendimmer, 0)
		sendCommand (Huiskamerdimmer, 0)
		sendCommand (Keukenled,OFF)
		
        }
        case 3: {
          sendCommand (Huiskamer_lichten, ON)
		sendCommand (Kitchendimmer, 100)
		sendCommand (Huiskamerdimmer, 100)
		sendCommand (KitchenKleur, 100)
		sendCommand (Keukenled,OFF)
		
        }
        
      
        default: { 
            logInfo("Mijnscenes","incorrect state: {}",Mijnscenes.state)
         
        
    
        }
        
    }
end

Sitemap:

Frame label= Scenes
		{
		Switch item=Mijnscenes mappings=[1="Cooking",2="Chilling",3="Working"] labelcolor=["gold"]
		Switch item=myScenes2 mappings=[1="Wake Up",2="Going To Sleep"]
		}

The problem is exactly what it says.
You have a string “someItem.state.toSring”, which is not a suitable command for a switch. .
It’s just a string.
It might look like an Item name etc., but it’s not going to get evaluated.
It’s just a string, exactly like “xxx” or “stop system now”

If you have a string and you would like to get the Item that has a name of that string 


1 Like

@rossko57,

thanks for that, I am now trying to find how to rework that into my case. Will let know if I am succesfull.

kr

Han

Thx @brizzik, my setup looks like yours, but I do not use fixed values. I have items defined that carry a value for a specific scene. Like

sendCommand(Light_1, Light_1_value_scene1)

with the help of the topic provided by @rossko57 I now have a working solution:

sendCommand(Light_VH_Woonkamer_Leeslampje, ScriptServiceUtil.getItemRegistry.getItem("Light_VH_Woonkamer_Leeslampje_S" + receivedCommand).state)

This will send the value Light_VH_Woonkamer_Leeslampje_S1 in case of scene 1 to my light Light_VH_Woonkamer_Leeslampje, Light_VH_Woonkamer_Leeslampje_S2 in case of scene 2 etc.