G’day all,
I am chasing a little help with converting a simple rule that I have made using sharptools to the Smartthings rules API so that it will run locally. I have never coded and I’m trying to teach myself using whatever resources I can but unfortunately I haven’t been able to find any examples to help me with this one. I will be wanting to expand on this rule but my first goal is just trying to get the basics to work and then play around with it.
The basics of the rule is to turn my kitchen lights on or off whenever I press either of the 2 buttons on an ikea tradfri 2 button remote. I would run this with just a normal automation except I also have a routine that if I hold the button then 1 light comes on dimly (for first thing in the morning so I don’t wake the family) and if I just use the on/off control in the smartthings app then once this routine has been run then the lights are out of sync and pressing a button turns one light off and the other light on.
What I am trying to achieve is:
if:
button 1 or button 2 is pressed
and:
light 1 is on
then:
turn light 1 and light 2 off
else:
turn light 1 and light 2 on
I found an example code here Rules | SmartThings Developers (last code at the bottom of the page) that I copied as the template for my rule and I replaced the device ID for the button and lights. I haven’t worked out how to write the code for button 1 OR button 2 so I’m just using button 1 as the trigger. I tried posting the request but I am getting a 422 unprocessable entity error saying the request was well formed but was unable to be followed due to semantic errors when I try posting from postman.
This is the code that i have written
{
"name": "Kitchen Light Button",
"actions": [
{
"if": {
"equals": {
"left": {
"string": "pushed"
},
"right": {
"device": {
"devices": [
"8d67fa9d-1301-4f3a-9ae5-89018fee29e9"
],
"component": "main",
"capabilities": "Button",
"attribute": "button1",
"trigger": "Always"
}
}
},
"then": [
{
"if": {
"equals": {
"left": {
"device": {
"devices": [
"cdc4574a-34b6-48a2-b5f4-862c90e08ce9"
],
"component": "main",
"capability": "switch",
"attribute": "switch",
"trigger": "Never"
}
},
"right": {
"string": "on"
}
},
"then": [
{
"command": {
"devices": [
"cdc4574a-34b6-48a2-b5f4-862c90e08ce9",
"3ebeda22-a75d-42f2-b873-c29c5878a602"
],
"commands": [
{
"component": "main",
"capability": "switch",
"command": "off",
"arguments": []
}
]
}
}
],
"else": [
{
"command": {
"devices": [
"cdc4574a-34b6-48a2-b5f4-862c90e08ce9",
"3ebeda22-a75d-42f2-b873-c29c5878a602"
],
"commands": [
{
"component": "main",
"capability": "switch",
"command": "on",
"arguments": []
}
]
}
}
]
}
}
]
}
}
]
}
and here is the device information for the tradfri 2 button remote
{
"deviceId": "8d67fa9d-1301-4f3a-9ae5-89018fee29e9",
"name": "two-buttons-battery",
"label": "Testing IKEA Remote Control",
"manufacturerName": "SmartThingsCommunity",
"presentationId": "ffec9b59-549b-3375-8d67-91b6896488eb",
"deviceManufacturerCode": "IKEA of Sweden",
"locationId": "248cca1b-5aa0-435b-b850-3b291b49474e",
"roomId": "8b6ac263-31dd-44e5-b1ef-414674e9ea8b",
"components": [
{
"id": "main",
"label": "main",
"capabilities": [
{
"id": "battery",
"version": 1
},
{
"id": "firmwareUpdate",
"version": 1
},
{
"id": "refresh",
"version": 1
},
{
"id": "universevoice35900.log",
"version": 1
}
],
"categories": [
{
"name": "RemoteController",
"categoryType": "manufacturer"
}
]
},
{
"id": "button1",
"label": "button1",
"capabilities": [
{
"id": "button",
"version": 1
}
],
"categories": [
{
"name": "RemoteController",
"categoryType": "manufacturer"
}
]
},
{
"id": "button2",
"label": "button2",
"capabilities": [
{
"id": "button",
"version": 1
}
],
"categories": [
{
"name": "RemoteController",
"categoryType": "manufacturer"
}
]
}
],
"createTime": "2023-07-25T03:23:16.996Z",
"parentDeviceId": "59c79abb-70a4-4bbc-8f51-609bb758031d",
"profile": {
"id": "e7d8d1be-99d8-38ed-a63d-d39c0b42f73c"
},
"zigbee": {
"eui": "842E14FFFEFEA759",
"networkId": "0FA3",
"driverId": "ad9f537d-e2d9-4354-8673-471a5306fad8",
"executingLocally": true,
"hubId": "59c79abb-70a4-4bbc-8f51-609bb758031d",
"provisioningState": "PROVISIONED"
},
"type": "ZIGBEE",
"restrictionTier": 0,
"allowed": null
}
I really appreciate any help as I’m totally new to this and just trying to get my head around the basics so I can work my way up from here.