Rule Engine - Copy functions

I could not find away to copy if statements or actions that are similar and repeat. Did i miss that or is it not available?

For some background. My use case is I am working on a way to bring in the Wyze sensors to a dashboard, and its rather combersome. With no integration able to collect that data form Wyze, i was left with the following.

Wyze is able to work with Google Home, But since thats a closed eco system as well here is how i accomplished it.

Google Home, is connected to IFTTT
Create Virtual Contact Sensors in SmartThings for each Wyze sensor
Create a rule for sensor open and another for sensor closed in IFTTT that triggers for all sensors and sends a webhook to a rule in SharpTools.
This rule is rather large becouse the only way i could find to do it was a IF statement for each device.
That statement then updates the status of the smartthings virtual sensor that can then be available for more typical window and door automation.

Where it would have been nice to use a copy was in the large if statement. I only needed to change one or two things, instead of a bran new if statement.

Neat idea and no it’s not currently available, but feel free to create a feature request for it!

Since you’re already into the some of the intermediate-advanced features with Webhooks, another advanced approach, which would significantly simplify the number of ‘redundant’ actions would be to use an Expression to determine which device ID you want to update (based on the input data received from IFTTT) and then use the SmartThings REST API directly (within your rule in an HTTP Action) to update the virtual device.

Here’s an example. As alluded to above, it’s using a number of advanced features, so feel free to ask about any of the steps and I’m happy to clarify what’s happening:

The main concept is:

  1. Set a variable $TargetDeviceId using an expression that defines a mapping of the Device Name (per IFTTT) to the relevant device ID that we want to control in SmartThings. The last line of this picks from the mapping based on the device name received in the webhook.

    Example expressions

    Device ID Mapping

    deviceIds = {
      "Front Door": "1XXX-XXXX-XXXX-XXXX",
      "Garage Door": "2XXX-XXXX-XXXX-XXXX",
      "Patio Door": "3XXX-XXXX-XXXX-XXXX",
      "Window - MasterBed1": "4XXX-XXXX-XXXX-XXXX",
      "Window - MasterBed2": "5XXX-XXXX-XXXX-XXXX",
    }
    
    deviceIds[$context.event.params.device]
    

    Device State (command)

    equalText($context.event.params.state, "Open") ? "on" : "off"
    
  2. Set a variable $TargetDeviceState using an expression that converts the ‘Open’ / ‘Closed’ state to ‘on’ and ‘off’ values. This uses a ternary condition: condition ? trueValue : falseValue since we only have two states to compare, but you could use the mapping approach from step one if you’re more comfortable with that.

    Example Content for easier copy-paste

    URL:

    https://api.smartthings.com/devices/{{$TargetDeviceId}}/commands
    

    Payload:

    {
      "commands": [
        {
          "component": "main",
          "capability": "switch",
          "command": "{{$TargetDeviceState}}",
          "arguments": []
        }
      ] 
    }
    
  3. The last step calls the SmartThings REST API directly and uses our $TargetDeviceId in the URL to indicate which device we’re updating and then sends the appropriate command using our $TargetDeviceState using the format the SmartThings API expects

    For demonstration, I used a variable $SmartThingsToken which doesn’t actually exist in my account just to show how you might setup the authorization header.

2 Likes

Thanks Josh! I like that idea, that would greatly simplify my approach. I will play with that! Thanks for the heads up and detailed example!