Usage question: setting a command on a device based on the state of another device

I would like to turn a switch on/off based on the current state of another device.

The main logic would be: if a switch state (on/off) changed, then mirror the new state on another switch. I know that could use IF statement to decide if the new state is on or off, but I am trying to simply issue the command (on/off) based on the state of the other switch. This is what I tried, but it’s not accepted. In other words, how do I make the switch command based on the setting of another switch.

{
“name”: “Mirror on/off of Laundry from Button (3)”,
“actions”: [
{
“if”: {
“changes”: {
“operand”: {
“device”: {
“devices”: [
“5270c846-914d-4bd7-917f-11d23c63e45c”
],
“component”: “switch3”,
“capability”: “switch”,
“attribute”: “switch”
}
}
},
“then”: [
{
“command”: {
“devices”: [
“e1a90f5e-3faa-4fa4-af03-70a4d9af5fc8”
],
“commands”: [
{
“component”: “main”,
“capability”: “switch”,
“command”: [
{
“device”: {
“devices”: [
“5270c846-914d-4bd7-917f-11d23c63e45c”
],
“component”: “switch3”,
“capability”: “switch”,
“attribute”: “switch”
}
}
]
}
]
}
}
]
}
}
]
}

The code being written in this thread might help out.

1 Like

Yes, I agree that I can take this approach which roughly translates to:

If switch1 state changes and it’s off, then set switch 2 state to off.
If switch1 state changes and it’s on, then set switch 2 state to on.

Yes, I can do that. But I was trying to figure out if there is a construct where the switch 2 command could be based on the state of switch 1.

I looked anywhere for the language syntax definition of Rules, but it seems that we need to create it by modifying examples.

But this will allow me to build a rule for now.

Thanks

1 Like

I’ve had the post below bookmarked in case I ever cared to get into rules. The second example sets an argument to a setter function based on the state of another device. Not quite what you’re looking for since it’s an argument instead of the command that’s being dynamically set, but maybe there’s something useful in there.

1 Like

Thank you. I will use that and hope that Rules continues to improve.

Also, Smartlighing can sync behaviors between devices.

Too simplistic…

Here is example syntax

{
  "name": "Switch 1 -> Switch 2",
  "actions": [
    {
      "if": {
        "equals": {
          "right": {
            "device": {
              "devices": [
                "switch 1 id"
              ],
              "component": "main",
              "capability": "switch",
              "attribute": "switch"
            }
          },
          "left": {
            "string": "on"
          }
        },
        "then": [
         {
            "command": {
              "devices": [
                "switch 2 id",
              ],
              "commands": [
                {
                  "component": "main",
                  "capability": "switch",
                  "command": "on"
                }
              ]
            }
          }          
        ]
      }
    },
    {
      "if": {
        "equals": {
          "right": {
            "device": {
              "devices": [
                "switch 1 id"
              ],
              "component": "main",
              "capability": "switch",
              "attribute": "switch"
            }
          },
          "left": {
            "string": "off"
          }
        },
        "then": [
          {
            "command": {
              "devices": [
                "switch 2 id",
              ],
              "commands": [
                {
                  "component": "main",
                  "capability": "switch",
                  "command": "off"
                }
              ]
            }
          }
        ]
      }
    }
  ]
}

You have to change switch 1 id and switch 2 id to your device’s ids.

Thank you.