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”
}
}
]
}
]
}
}
]
}
}
]
}
HalD
(Hal)
March 31, 2023, 4:44pm
2
The code being written in this thread might help out.
@orangebucket ChatGPT provided me with this updated code for on/off based on your feedback:
[IMG_4399]
{
"name": "Sync Hue, Saturation, Brightness, and On/Off",
"actions": [
// ... (hue, saturation, and brightness syncing actions)
{
"if": {
"changes": {
"operand": {
"device": {
"devices": [
"master-bulb-id"
],
"component": "main",
"capability": "switch",
"attribute": "switch"
}
}
},
"equals": "on",
"then": [
{
"command": {
"devices": [
"bulb-1-id",
"bulb-2-id",
"bulb-3-id"
],
"component": "main",
…
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.
The engineering team mentioned the mirror functions can be achieved using the Rules API and the changes condition.
It is different depending on the capability configuration to set the attribute value.
For example, the Switch capability has two enum commands that don’t receive any arguments.
In this case, we need to check the change to a specific value (“on”/“off”) and send the same value to the other device.
This sample shows how we can mirror the switch events of a multi-component device that has a switch in two components (light and main), that’s also why only one deviceID is used.
{
"name":"Mirror switch events of main and light components",
"actions":[
{
"if":{
"changes":{
"equals":{
"left":{
…
1 Like
Thank you. I will use that and hope that Rules continues to improve.
Also, Smartlighing can sync behaviors between devices.
TapioX
March 31, 2023, 5:24pm
8
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.