[SOLVED] (Simply) question: "and" in a rule with a switch

Hi,
I just try to code a simply rule to add my Xiaomi cube …

Actual the rule is:

rule "Aqara Cube event"
when
    Channel 'mihome:sensor_cube:158d00029a93ff:action' triggered
then
    var actionName = receivedEvent.getEvent()
    switch(actionName) {
       case "MOVE": { logInfo("diverses.rules", "MOVE (Angel:{}",Xiaomi_Cube_Angle_01.state)
        ...
         ...

I already add a (virtual) switch to decide, if the action from the cube should be realy executed.
Switchname is: vSwitch_Cube

I just want to add that when a “action” from the cube was triggered, the vSwitch_Cube is also checked that his state is “ON” (when “OFF”, do no action form the defined cube actions)

Can I realise this in the same line with an “and”? How? With an additioally “if …”, OK, this will also work. …

I try this, but (clear) is not working…

rule "Aqara Cube event"
when
    Channel 'mihome:sensor_cube:158d00029a93ff:action' triggered  and  Item vSwitch_Cube = ON
then
    var actionName = receivedEvent.getEvent()
    switch(actionName) {
       case ...
...
...

I know how to add “and” or “or” in rule like:

Item Switch_1 changed from ON to OFF or Item Switch_2 changed from CLOSED to OPEN

But how check if the state is “ON” (without an change from … to …) ?

Same procedure as every year… :wink: SCNR

The rule has two parts, the triggers and the code which is executed.

The trigger part is a list of every event that will start execution of the rule. There is no “but only if” in this part.
If there would be an and to link conditions, the rule would only trigger if both triggers happen at the same time (which is very unlikely ever to happen).

So instead you have to check the state within the executed code:

rule "Aqara Cube event"
when
    Channel 'mihome:sensor_cube:158d00029a93ff:action' triggered
then
    if(vSwitch_Cube.state != ON) 
        return;                   // stop rule immediately
    var actionName = receivedEvent.getEvent()
    switch(actionName) {
       case ...
...
...
1 Like

I made a tutorial we can link to next time.

1 Like

OK, so it’s not possible to use here (triggered based on events) an “and”.
So i will use the additional if-section that Udo provided, thx!

Hi Martin, I thumbs in your examples that interested me
Can you better explain the virtual switch purpose? How do you make use of it?
Perheaps like as variable to store item state? How do you use it?

I’m sorry,
can’t find my “Old” solution, I think it was a “simple” “if” construct inside an rule …
Actuall I use the “Global” solution from Udo inside the Cube-rule, it works fine:
“Execute the cube movement -action only, when the vSwitch_Cube.state is ON”:
(in 5th and 6th line)

rule "Aqara Cube event"
when
    Channel 'mihome:sensor_cube:158d00029a93ff:action' triggered
then
    if(vSwitch_Cube.state != ON) 
        return;                   // stop rule immediately
    var actionName = receivedEvent.getEvent()
    switch(actionName) {
       case ...

Can I ask you to submit your entire code?
I hardly understand the logic behind the fact that, for me, every “action” trigger differently, means for example different light lamps
Perheaps reading the entire example would help me
Thanks so much

Shure, he it is (may “quick and dirty”, but working :wink: )
Thx to this forum, most code & help comes from here !

rule "Aqara Cube event #01"
when
    Channel 'mihome:sensor_cube:158d000xxxxxx:action' triggered
then
    if(vSwitch_Cube_01.state != ON)
         return;
    var actionName = receivedEvent.getEvent()
    switch(actionName) {
        case "MOVE": {
            logInfo("diverses.rules", "MOVE (Angel:{}",Xiaomi_Cube_Angle_01.state)
            if (Switch_Socket_14.state == ON )
               { Switch_Socket_14.sendCommand(OFF) }
            else
               { Switch_Socket_14.sendCommand(ON) }
        }
        case "ROTATE_RIGHT": {
            logInfo("test_rule.rules", "Cube #01: Roate right (Angel:{}",Xiaomi_Cube_Angle_01.state)
            Dimmer_Livingroom.sendCommand(0)
        }
        case "ROTATE_LEFT": {
            logInfo("test_rule.rules", "Cube #01: Rotate left (Angel:{}",Xiaomi_Cube_Angle_01.state)
            if ( Dimmer_Livingroom.state == 0 )
               { Dimmer_Livingroom.sendCommand(1) }
            else 
                if (Dimmer_Livingroom.state > 15 )
                  { Dimmer_Livingroom.sendCommand(1) }
                else
                   { Dimmer_Livingroom.sendCommand(100) }
        }
        case "FLIP90": {
            logInfo("test_rule.rules", "Cube #01: Flip 90 (Angel:{}",Xiaomi_Cube_Angle_01.state)
            logInfo("test_rule.rules", "          Rollershutter Wert SZ:{}", Rollershutter_Livingroom_blinds.state)
            if (Rollershutter_Livingroom_blinds.state > 60 )
               { Rollershutter_Livingroom_blinds.sendCommand(0) }
            else
               { Rollershutter_Livingroom_blinds.sendCommand(69) }
        }
        case "FLIP180": {
            logInfo("test_rule.rules", "Cube #01: Flip 180 (Angel:{}",Xiaomi_Cube_Angle_01.state)
            Rollershutter_Parentsbedroom_blinds.sendCommand(90)
        }
        case "TAP_TWICE": {
            logInfo("test_rule.rules", "Cube #01: Tap Twice (Angel:{}",Xiaomi_Cube_Angle_01.state)
            logInfo("test_rule.rules", "Cube #01: Flip 180 (Sock 07:{}",Switch_Socket07.state)
            if (Switch_Socket07.state == ON )
               { Switch_Socket07.sendCommand(OFF) }
            else
               { Switch_Socket07.sendCommand(ON) }
        }
        case "SHAKE_AIR": {
            logInfo("test_rule.rules", "Cube #01: Shake Air (Angel:{}",Xiaomi_Cube_Angle_01.state)
            val vPushoverImage = "/srv/openhab2-userdata/snap.cgi?chn=0"
            executeCommandLine("rm /srv/openhab2-userdata/snap.cgi?chn=0")
            executeCommandLine("wget -o /tmp/wget.out http://viewer:viewer298@192.168.xx.xx//mjpeg/snap.cgi?chn=0")
            Thread::sleep(4000)
            sendPushoverMessage(pushoverBuilder("Abfrage Gartenkamera (mittels Cube-shake)")
               .withApiKey("a4jm6ecxohkhxxxxxxxxxxxxxxxxxx").withAttachment(vPushoverImage))
        }
        case "FREE_FALL": {
            logInfo("test_rule.rules", "Cube #01: Free Fall (Angel:{}",Xiaomi_Cube_Angle_01.state)
        }
        case "ALERT": {
            logInfo("test_rule.rules", "Cube #01: Alert (Angel:{}",Xiaomi_Cube_Angle_01.state)
        }
    }
end

Ok, very good example, really so usefully especially for the if/then/else conditions inside that confim me the right way considered my code developed like this

rule "Aqara Cube Control"

when
    Channel "mqtt:topic:mosquitto:testthecube:action" triggered // In abbinata al valore trigger=true nella configurazione del channel

then
       logInfo("test_AqaraCube rules", " Channel triggerd: {} ", receivedEvent.getEvent)  // test
//       logInfo("test_channel.rules", " Channel triggerd: {} ", receivedEvent)  // test

var actionName = receivedEvent.getEvent()


logInfo( "CUBO", "INFO: Channel Event = '{}'", actionName )

    switch(actionName)   {

        case "shake": {
                if (Mobile_Stato.state.toString != "ON") // != vuol dire Not Equal
   Mobile_Stato.sendCommand(ON)
                else 
   Mobile_Stato.sendCommand(OFF)
        }

        case "flip90":{
                if(Cucina_Stato.state.toString != "ON")
   Cucina_Stato.sendCommand(ON)
                else
   Cucina_Stato.sendCommand(OFF)
        }

        case "flip180":{
                if(Shelly_Switch.state.toString != "ON")
                Shelly_Switch.sendCommand(ON)
                else
                Shelly_Switch.sendCommand(OFF)
        }

        case "rotate_left":{
                if(GTutteLeRampe.state.toString !="ON")
        GTutteLeRampe.sendCommand(ON)
                else
        GTutteLeRampe.sendCommand(OFF)
        }

        case "rotate_right":{
                if(SW_Ikea_Ingress.state.toString != "ON")
        SW_Ikea_Ingress.sendCommand(ON)
                else
        SW_Ikea_Ingress.sendCommand(OFF)
        }

        case "slide":{
                if(Toilet.state.toString != "ON")
        Toilet.sendCommand(ON)
                else
        Toilet.sendCommand(OFF)
        }

        default:
{   logInfo("xiaomi", "No match found for {}", actionName)}
    }
AqaraCubeAction.postUpdate(actionName)
end

Perheaps, better reading the example I find a reason for the vSwitch item in your code
Is this binding related? Better, is this gateway related so that you have to check the CUBE status at any time?
I didn’t understand it before because my code is MQTT and Zigbee framework dependent.
Also, this explain the different tag I have on my Awara Cube
Am I right?