Rule Syntax Help - Encoding a on/off condition

Here is a rule snippet that I am trying to create a rule from. Specifically, I want to apply the rule if the switch comes on or off. I can’t seem to find the correct syntax for the right side. I just included a small piece of the rule and I need to figure out how to specify the right side correctly. I also included the full rule if that is easier to work with.

The intended rule states: If Switch 1 changes to ON or OFF, then execute a routine and turn on the switch. I am stuck with how to encode the ON or OFF condition.

  "if": {
    "changes": {
      "equals": {
        "left": {
          "device": {
            "devices": [
              "17ec5cb1-25a0-4923-8fa7-f5b6c95521d6"
            ],
            "component": "main",
            "capability": "switch",
            "attribute": "switch"
          }
        },
        "right": {
          "string": "off"
        }
      }
    },

or the full rule as it exists right now.

{
    "name": "test",
    "actions": [
        {
  "if": {
    "changes": {
      "equals": {
        "left": {
          "device": {
            "devices": [
              "17ec5cb1-25a0-4923-8fa7-f5b6c95521d6"
            ],
            "component": "main",
            "capability": "switch",
            "attribute": "switch"
          }
        },
        "right": {
          "string": "off"
        }
      }
    },
    "then": [
      {
        "scene": {
          "scenes": [
            "d733090e-2ff5-4212-8472-fbc9b3ed0c63"
          ]
        }
      },
      {
        "command": {
          "devices": [
            "8c350809-d794-4e44-abcc-102d253e8bc9"
          ],
          "commands": [
            {
              "component": "main",
              "capability": "switch",
              "command": "on"
            }
          ]
        }
      }
    ]
  }
}       
    ]
}

Solved. Thanks @orangebucket.

    {
      "if": {
        "changes": {
          "operand": {
            "device": {
              "devices": [
                "17ec5cb1-25a0-4923-8fa7-f5b6c95521d6"
              ],
              "component": "main",
              "capability": "switch",
              "attribute": "switch"
            }
          }
        },
1 Like

I’d suggest …

  "if": {
    "changes": {
      "operand": {
        "device": {
            "devices": [
              "17ec5cb1-25a0-4923-8fa7-f5b6c95521d6"
            ],
            "component": "main",
            "capability": "switch",
            "attribute": "switch"
          }
        
      }
    },

I don’t know what the exact details are, but the operand seems to satisfy the requirement of changes desire for a false to true transition when an event comes in that makes the content of the operand change. In fact I don’t know if it has to even change what it evaluates to, maybe evaluation suffices.

The longer way of doing it is to have one changes that uses equals on the switch being on, and one that uses equals on the switch being off, and then or them together. The key consideration about changes is that it requires a false to true transition.

I’m pretty sure that Routines have specific operands and actions to handle toggles, but they aren’t in the public Rules yet. I haven’t got around to confirming this.

Thank you, works like a champ.

Excellent, and it scratched an itch for me as I’ve been meaning to test it ever since @nayelyz helped me finally grasp what operand actually does. I still need to do some more testing, but I probably won’t. I mean I’ve just remembered that I made a mug of tea a few minutes ago and it is at risk of stewing.

1 Like