G’day all. I need some help writing a rules API script to sync 2 devices. I have a door lock that works through google and I’ve set up a virtual switch that syncs with the door lock status in google. This works well except as the switch is just a switch in smartthings i can’t utilise it in smartthings security. I set up a virtual lock in smartthings to use instead of the switch in google but this doesn’t show up in google. I’ve decided I’ll have to uses a virtual switch to link google to smartthings and vice versa but to get this to work with smartthings security I need to sync the virtual switch to the virtual lock.
anyway, I’ve gotten the virtual switch to virtual lock working one way with a rule stating that:
if virtual switch - on, then virtual lock - locked
else virtual lock - unlocked
using the following code where 892d6707-57db-42a1-afdc-67cd02dc39db is the switch and 3c90112b-adf1-4fb7-91ab-cbffde4de8f7 is the lock
{
"name": "If front door is switch changes, sync virtual door lock",
"actions": [
{
"if": {
"equals": {
"right": {
"device": {
"devices": [
"892d6707-57db-42a1-afdc-67cd02dc39db"
],
"component": "main",
"capability": "switch",
"attribute": "switch"
}
},
"left": {
"string": "on"
}
},
"then": [
{
"command": {
"devices": [
"3c90112b-adf1-4fb7-91ab-cbffde4de8f7"
],
"commands": [
{
"component": "main",
"capability": "lock",
"command": "lock"
}
]
}
}
],
"else": [
{
"command": {
"devices": [
"3c90112b-adf1-4fb7-91ab-cbffde4de8f7"
],
"commands": [
{
"component": "main",
"capability": "lock",
"command": "unlock",
"arguments": []
}
]
}
}
]
}
}
]
}
this works well but I can’t use the virtual lock to change the virtual switch state (which unfortunately due to security reasons in google I can only use to lock the door). I tried adding another if/then routine (if virtual switch - on, then virtual lock - locked, else virtual lock - unlocked, if virtual switch - off, then virtual lock - unlocked, else virtual lock - locked) under the first routine but that just put the whole thing into a loop. then I tried nesting if statements like this
{
"name": "If front door is switch changes, sync virtual door lock",
"actions": [
{
"if": {
"equals": {
"right": {
"device": {
"devices": [
"892d6707-57db-42a1-afdc-67cd02dc39db"
],
"component": "main",
"capability": "switch",
"attribute": "switch"
}
},
"left": {
"string": "on"
}
},
"then": [
{
"command": {
"devices": [
"3c90112b-adf1-4fb7-91ab-cbffde4de8f7"
],
"commands": [
{
"component": "main",
"capability": "lock",
"command": "lock"
}
]
}
}
],
"if": {
"equals": {
"right": {
"device": {
"devices": [
"892d6707-57db-42a1-afdc-67cd02dc39db"
],
"component": "main",
"capability": "switch",
"attribute": "switch"
}
},
"left": {
"string": "off"
}
},
"then": [
{
"command": {
"devices": [
"3c90112b-adf1-4fb7-91ab-cbffde4de8f7"
],
"commands": [
{
"component": "main",
"capability": "lock",
"command": "unlock"
}
]
}
}
],
"if": {
"equals": {
"right": {
"device": {
"devices": [
"3c90112b-adf1-4fb7-91ab-cbffde4de8f7"
],
"component": "main",
"capability": "lock",
"attribute": "lock"
}
},
"left": {
"string": "lock"
}
},
"then": [
{
"command": {
"devices": [
"892d6707-57db-42a1-afdc-67cd02dc39db"
],
"commands": [
{
"component": "main",
"capability": "switch",
"command": "on"
}
]
}
}
],
"else": [
{
"command": {
"devices": [
"892d6707-57db-42a1-afdc-67cd02dc39db"
],
"commands": [
{
"component": "main",
"capability": "switch",
"command": "off",
"arguments": []
}
]
}
}
]
}
}
}
}
]
}
but that didn’t work and I got the following error
{
"requestId": "4353716590818799289",
"error": {
"code": "ConstraintViolationError",
"message": "The request is malformed.",
"details": [
{
"code": "BodyMalformedError",
"target": "if",
"message": "Unrecognized field \"if\" (class v20190122.internal.st.behaviors.IfAction), not marked as ignorable",
"details": []
}
]
}
}
i tried swapping the second and third if statements to elseif but again it didn't like it.
I suppose I could probably put this into 2 rules but I get the feeling that if I did that it would just cause another loop. this should be such a basic rule that it should have an example in the sample rules like google does @nayelyz .