Help with Rules API - Can't get to work even with sample rules

Hello,
I was trying to find out, what am I doing wrong, but even when I use sample rules, it just does not work for me.
I am trying to do a rule to “copy” humidity value from air condition to virtual humidity sensor. But all the time I am getting just error like “Error adding rule “Test”. 422 The request is malformed. Unknown target: Malformed body on line 1:26”. I saw, that the position is affected by the rule name, which is because the JSON format see it like one row or something?
Can anyone tell me, please, where is the issue?
Not sure, if that matters, I have AEOTEC Hub.

{
    "name": "Sync Humidity from AC to Virtual Device",
    "actions": [
        {
            "if": {
                "device": {
                    "id": "7ed81500-61ce-cefe-76aa-cf198f94e73b",
                    "capability": "relativeHumidityMeasurement",
                    "attribute": "humidity",
                    "changes": "any"
                }
            },
            "then": [
                {
                    "command": {
                        "devices": [
                            "62d318d4-554e-49ca-9d25-d1f2911e5b71"
                        ],
                        "commands": [
                            {
                                "component": "main",
                                "capability": "relativeHumidityMeasurement",
                                "command": "setHumidity",
                                "arguments": [
                                    {
                                        "device": {
                                            "id": "7ed81500-61ce-cefe-76aa-cf198f94e73b",
                                            "capability": "relativeHumidityMeasurement",
                                            "attribute": "humidity"
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                }
            ]
        }
    ]
}

How are you creating the Rules? If you are pasting into the Advanced Web App there is a ‘gotcha’ because it only wants the actions array. So not the outer {}, not the name line, and not the "actions": key. Just the [ ... ].

However regardless of that your if action doesn’t look correct. To be testing for a change to a device attribute I would expect you to be using a changes operator, then an operand operator and the the device operand.

1 Like

Yes, I am using the Advanced Web App. I did not know, that there is any other “easy way”, because the CLI looks like far more advanced version for me.
Could you please point me where is the information written, so I could read it there?
Or write part of the code how it should look like in my case?

I think your code should start something like:

[
  {
    “if”: {
      “changes”: {
        “operand”: {
          “device”: {
            “devices”: [
              “7ed81500-61ce-cefe-76aa-cf198f94e73b”
            ],
            “component”: “main”,
            “capability”: “relativeHumidityMeasurement”,
            “attribute”: “humidity”,
            “trigger”: “Always”
          }
        }
      },
     “then”: []
    },

Update: Sorry I put the then property in the wrong place to start with.

1 Like

The relative humidity capability has no command (setHumidity). You have to use virtual device that supports humidity setting.

Yes, sorry, I forgot to edit that part. I am using virtual device, which can do the setHumidity via setvHumidity command.
I am trying to figure it out, so far no luck, I need to take a look into it later again. I never did anything in JSON before to be honest. I know, that the formatting is wrong now. If anyone can help me to correct my mistake, I would really appreciate that.
And thank you guys for your help so far! :slight_smile:

[
	{
    “if”: {
      “changes”: {
        “operand”: {
          “device”: {
            “devices”: [
              “7ed81500-61ce-cefe-76aa-cf198f94e73b”
            ],
            “component”: “main”,
            “capability”: “relativeHumidityMeasurement”,
            “attribute”: “humidity”,
            “trigger”: “Always”
          }
        }
      }
    },
    “then”: {
		[
			"command": {
				"devices": [
					"e6080378-88d1-4a21-8284-8c3be00c3309"
				],
				"commands": [
					{
						“component”: "main”,
						“capability”: "partyvoice23922”,
						“command": "setvHumidity”,
						“arguments”: [
							{
								“device”: {
									“id”: "7ed81500-61ce-cefe-76aa-cf198f94e73b",
									“component”: “main”,
									“capability”: “relativeHumidityMeasurement”,
									“attribute”: “humidity”,
								}
							}
						]
					}
				]
			}
		}
	]
	}
]

Fix to

“capability”: “partyvoice23922.vhumidityset”,
“command”: “setvHumidity”,

1 Like

I was trying a lot of things, I have no idea, what is wrong. When I put the code below, I get

Error updating rule logic. 422 The request is malformed. then: Unrecognized field “then” (class v20190122.internal.st.behaviors.Action), not marked as ignorable

[
  {
    "if": {
      "changes": {
        "operand": {
          "device": {
            "devices": [
              "7ed81500-61ce-cefe-76aa-cf198f94e73b"
            ],
            "component": "main",
            "capability": "relativeHumidityMeasurement",
            "attribute": "humidity",
            "trigger": "Always"
          }
        }
      }
    },
    "then": [
      {
        "command": {
          "devices": [
            "e6080378-88d1-4a21-8284-8c3be00c3309"
          ],
          "commands": [
            {
              "component": "main",
              "capability": "partyvoice23922.vhumidityset",
              "command": "setvHumidity",
              "arguments": [
                {
                  "device": {
                    "devices": [
                      "7ed81500-61ce-cefe-76aa-cf198f94e73b"
                    ],
                    "component": "main",
                    "capability": "relativeHumidityMeasurement",
                    "attribute": "humidity"
                  }
                }
              ]
            }
          ]
        }
      }
    ]
  }
]

When I tried:

[
  {
    "if": {
      "changes": {
        "operand": {
          "device": {
            "devices": [
              "7ed81500-61ce-cefe-76aa-cf198f94e73b"
            ],
            "component": "main",
            "capability": "relativeHumidityMeasurement",
            "attribute": "humidity",
            "trigger": "Always"
          }
        }
      }
    }
  }
]

That worked.

If I add just empty “then” to it, same error like I wrote at the top.

[
  {
    "if": {
      "changes": {
        "operand": {
          "device": {
            "devices": [
              "7ed81500-61ce-cefe-76aa-cf198f94e73b"
            ],
            "component": "main",
            "capability": "relativeHumidityMeasurement",
            "attribute": "humidity",
            "trigger": "Always"
          }
        }
      }
    },
    "then": []
  }
]

I haven’t paid attention at all, but maybe try this:

[
  {
    "if": {
      "changes": {
        "operand": {
          "device": {
            "devices": [
              "7ed81500-61ce-cefe-76aa-cf198f94e73b"
            ],
            "component": "main",
            "capability": "relativeHumidityMeasurement",
            "attribute": "humidity",
            "trigger": "Always"
          }
        }
      }
    },
    "type": "equals",
    "then": []
  }
]

With this, I get:

Error updating rule logic. 422 The request is malformed. type: Unrecognized field “type” (class v20190122.internal.st.behaviors.Action), not marked as ignorable

Try this: create a routine with the devices that comes as close as possible to what you try to accomplish in the SmartThings app.

Then edit that rule in AWA (“show JSON view”, copy that into a new rule).

This way you can make sure that the syntax and structure is correct.

That I tried like the first, before I wrote here. I am getting the same errors :frowning:

1 Like

The if action object is an object with a number of properties. The three main ones are: a condition such as changes, equals or and; then; and else.

So the structure in AWA is:

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

I know that isn’t what I wrote earlier but I don’t write Rules in JSON and got so tangled in the formatting using a mobile that I put the then in the wrong place.

The type key is one of a few things that appear in Routines but not the Rules API. It always appears as the last property in the JSON objects, despite the order of the properties not otherwise being important. I would imagine this creates a more rigid structure to the JSON that facilitates recreating the Routines in the app.

2 Likes

I tried to fix this version of your Rules API rule and for me this syntax was ok (using my IDs)

{
    "if": {
      "changes": {
        "operand": {
          "device": {
            "devices": [
              "7ed81500-61ce-cefe-76aa-cf198f94e73b"
            ],
            "component": "main",
            "capability": "relativeHumidityMeasurement",
            "attribute": "humidity",
            "trigger": "Always"
          }
        }
    },
    "then": [
	  {
			"command": {
				"devices": [
					"e6080378-88d1-4a21-8284-8c3be00c3309"
				],
				"commands": [
					{
						"component": "main",
						"capability": "partyvoice23922.vhumidityset",
						"command": "setvHumidity",
						"arguments": [
							{
								"device": {
									"devices": [
									  "7ed81500-61ce-cefe-76aa-cf198f94e73b"
									],
									"component": "main",
									"capability": "relativeHumidityMeasurement",
									"attribute": "humidity",
								}
							}
						]
					}
				]
			}
		}
      ]
	}
}

Could you please confirm if this rule works for you.

This rule format is for AWA (paste json to yellow marked place)

2 Likes

Yes, that works too. Based on previous answers I also created working rule. Thank you for help from all of you! :slight_smile:

[
  {
    "if": {
      "changes": {
        "operand": {
          "device": {
            "devices": [
              "7ed81500-61ce-cefe-76aa-cf198f94e73b"
            ],
            "component": "main",
            "capability": "relativeHumidityMeasurement",
            "attribute": "humidity",
            "trigger": "Always"
          }
        }
      },
      "then": [
        {
          "command": {
            "devices": [
              "e6080378-88d1-4a21-8284-8c3be00c3309"
            ],
            "commands": [
              {
                "component": "main",
                "capability": "partyvoice23922.vhumidityset",
                "command": "setvHumidity",
                "arguments": [
                  {
                    "device": {
                      "devices": [
                        "7ed81500-61ce-cefe-76aa-cf198f94e73b"
                      ],
                      "component": "main",
                      "capability": "relativeHumidityMeasurement",
                      "attribute": "humidity"
                    }
                  }
                ]
              }
            ]
          }
        }
      ]
    }
  }
]
2 Likes