422 Errors when Trying to Control Hood Fan Speed with Virtual Dimmer Switch

Hey there! I’m attempting to build a virtual dimmer switch that i can expose to HomeAssistant to control my Microwave Hood Fan Speed through automations. I.E, if the oven turns on, always turn on the fan etc.

I’m making a rule to tie my virtual switch to my hood fan, but the rule keeps failing to save citing that my target for the command is invalid. My sample json is below:

[  {
    "if": {
      "equals": {
        "left": {
          "device": {
            "devices": ["c8651de9-7097-4dee-abd8-06adc6eba038"],
            "component": "main",
            "capability": "switchLevel",
            "attribute": "level"
          }
        },
        "right": 0
      }
    },
    "then": [
      {
        "command": {
          "devices": ["ad4ac869-5799-9911-5b98-6b5e656f0dd5"],
          "commands": [
            {
              "component": "hood",
              "capability": "samsungce.hoodFanSpeed",
              "command": "setHoodFanSpeed",
              "arguments":[0]
            }
          ]
        }
      }
    ]
  }]

The error is this: Error adding rule “Microwave Fan Speed”. 422 The request is malformed. Unknown target: Malformed body on line 1:204

Anyone point me in the right direction? Screenshot of what i’m seeing is attached.

@orangebucket can answer that.

Despite what you may still see in the API Reference, arguments needs an array of JSON objects. So rather than [ 0 ] I would try [ { "integer": 0 } ] and see if that is what made it barf.

Oh hang on, the "right": 0 probably needs fixing first otherwise you’ll get the same error. That needs to be { "integer": 0 }.

Oh and I’ve just seen another issue. Both equals and then should be properties of the if action object. So the object hierarchy should look like.

  if
      equals
          left
          right
      then
          command

I think you’ve got the then at the same level as the if.

2 Likes

You are a saint! OF COURSE samsung doesn’t mention that.

So now i have this:

[
  {
  "if": {
    "equals": {
      "left": {
        "device": {
          "devices": ["c8651de9-7097-4dee-abd8-06adc6eba038"],
          "component": "main",
          "capability": "switchLevel",
          "attribute": "level"
        }
      },
      "right": { "integer": 0 }
    }
  },
  "then": [
    {
      "command": {
        "devices": ["ad4ac869-5799-9911-5b98-6b5e656f0dd5"],
        "component": "hood",
        "capability": "samsungce.hoodFanSpeed",
        "command": "setHoodFanSpeed",
        "arguments": [{"integer": 0}]
      }
    }
  ]
}
]

And am getting this new error: Error adding rule “Microwave Fan Speed”. 422 The request is malformed. then: Unrecognized field “then” (class v20190122.internal.st.behaviors.Action), not marked as ignorable

Which doesn’t really make sense. But then i moved the then inside the if block as you mentioned, like this:

[
  {
  "if": {
    "equals": {
      "left": {
        "device": {
          "devices": ["c8651de9-7097-4dee-abd8-06adc6eba038"],
          "component": "main",
          "capability": "switchLevel",
          "attribute": "level"
        }
      },
      "right": { "integer": 0 }
      
    },
    "then": [
    {
      "command": {
        "devices": ["ad4ac869-5799-9911-5b98-6b5e656f0dd5"],
        "component": "hood",
        "capability": "samsungce.hoodFanSpeed",
        "command": "setHoodFanSpeed",
        "arguments": [{"integer": 0}]
      }
    }
  ]
  }
}
]

and got this: Error adding rule “Microwave Fan Speed”. 422 The request is malformed. component: Unrecognized field “component” (class v20190122.internal.st.behaviors.CommandAction), not marked as ignorable

I’m looking at another thread where this came up but not seeing the problem: Automating On when it is Off, periodically. My code will always give an error

I think you had the basic structure of the command action right to start with (apart from the arguments).

oh duh

That worked! @orangebucket you are a gem. Working example code here for any future travelers:

[
  {
    "if": {
      "equals": {
        "left": {
          "device": {
            "devices": [
              "c8651de9-7097-4dee-abd8-06adc6eba038"
            ],
            "component": "main",
            "capability": "switchLevel",
            "attribute": "level"
          }
        },
        "right": {
          "integer": 0
        }
      },
      "then": [
        {
          "command": {
            "devices": [
              "ad4ac869-5799-9911-5b98-6b5e656f0dd5"
            ],
            "commands": [
              {
                "component": "hood",
                "capability": "samsungce.hoodFanSpeed",
                "command": "setHoodFanSpeed",
                "arguments": [
                  {
                    "integer": 0
                  }
                ]
              }
            ]
          }
        }
      ]
    }
  }
]
1 Like