Please sovle my rule api problem

I’ve try to make rule api as below. But I just get error as “Error adding rule “Aqara H1”. 422 The request is malformed. then: Unrecognized field “then” (class v20190122.internal.st.behaviors.Action), not marked as ignorable”

Please let me know how to handle it or modify my api…

[
   {
      "if": {
         "changes": {
                  "operand": {
                     "device": {
                        "devices": [
                           "270aa5a2-b83f-47aa-b5ef-2096517bdc89"
                        ],
                        "component": "main",
                        "capability": "switchLevel",
                        "attribute": "level"
                     }
                  }
               }
            },
         "then": [
            {
               "if": {
                  "equals": {
                     "left": {
                        "device": {
                           "devices": [
                              "46d958cb-a165-4b6e-bc45-172cef6f2615"
                           ],
                           "component": "main",
                           "capability": "switch",
                           "attribute": "switch",
                           "trigger": "Never"
                        }
                     },
                     "right": {
                        "string": "on"
                     }
                  }
               },
                 "then": [
                     {
                        "command": {
                           "devices": [
                              "46d958cb-a165-4b6e-bc45-172cef6f2615"
                           ],
                           "commands": [
                              {
                                 "component": "main",
                                 "capability": "switchLevel",
                                 "command": "setLevel",
                                 "arguments": [
                                    {
                                       "device": {
                                          "devices": [
                                             "270aa5a2-b83f-47aa-b5ef-2096517bdc89"
                                          ],
                                          "component": "main",
                                          "capability": "switchLevel",
                                          "attribute": "level"
                                    }
                                 }
                              ]
                           },
                         ]
                      }  
                    }
               ]
            }
       ]
   }
 ]

Please add ``` before and after the code, like this:

```
type or paste code here
```

There may be other issues as the JSON didn’t validate when I tried it, but what that error is saying is that it encountered a then where it wasn’t expecting it. If looks to me like you’ve got the then at the same nesting level as the if. The basic structure you should be using when pasting code into the Advanced Web App to create Rules is:

[
    {
        "if": {
            "changes": { ... }
            "then":    [ { ... }, { ... } ]
            "else":    [ { ... }, { ... } ]
        }
    }
]

The if is the action object and it contains the then and else result arrays and a condition object (such as changes or equals).

For the benefit of other readers who do not use the Advanced Web App, the above represents the actions array of a Rule. So the whole Rule would have the format:

{
    "name": "Name of the rule",
    "actions": [ ... ]
}

where the [ ... ] is the code structure shown previously.

I personally don’t like the way the AWA handles Rules entry as it doesn’t allow for the sequence property for the actions array. Not that I write Rules in JSON anyway.

1 Like

Found a number of issues, including the error Graham noted, which you repeated in the second nested if-then. I made the corrections and validated the json, but didn’t test the rule itself.

{
    "name": "Your Rule Name",
    "actions": [
        {
            "if": {
                "changes": {
                    "operand": {
                        "device": {
                            "devices": [
                                "270aa5a2-b83f-47aa-b5ef-2096517bdc89"
                            ],
                            "component": "main",
                            "capability": "switchLevel",
                            "attribute": "level"
                        }
                    }
                },
                "then": [
                    {
                        "if": {
                            "equals": {
                                "left": {
                                    "device": {
                                        "devices": [
                                            "46d958cb-a165-4b6e-bc45-172cef6f2615"
                                        ],
                                        "component": "main",
                                        "capability": "switch",
                                        "attribute": "switch",
                                        "trigger": "Never"
                                    }
                                },
                                "right": {
                                    "string": "on"
                                }
                            },
                            "then": [
                                {
                                    "command": {
                                        "devices": [
                                            "46d958cb-a165-4b6e-bc45-172cef6f2615"
                                        ],
                                        "commands": [
                                            {
                                                "component": "main",
                                                "capability": "switchLevel",
                                                "command": "setLevel",
                                                "arguments": [
                                                    {
                                                        "device": {
                                                            "devices": [
                                                                "270aa5a2-b83f-47aa-b5ef-2096517bdc89"
                                                            ],
                                                            "component": "main",
                                                            "capability": "switchLevel",
                                                            "attribute": "level"
                                                        }
                                                    }
                                                ]
                                            }
                                        ]
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        }
    ]
}

1 Like