Using $context Values in Expression

So was running through the context information from the site. I see it all was using $context.event and not device.

If anyone has full / better documentation for all uses of Context, that would be great. But here is what I understand from it…

$context is used to grab values of a device’s properties. Example, my EcoBee thermastat in my shop is called ‘Workshop’. I am able setup a CONDITION to check if the workshop’s thermostatMode is NOT OFF.

If it is NOT OFF, I can set a variable with the value of thermostatMode and some other information to send via PUSH notification. Example that I use and populates properly is:

Set variable $vTXTShopGroupMessage value using expression:
concat($vTXTShopGroupMessage , ‘therm@’ , $context.Workshop.thermostatMode)

So it basically sets the GroupMessage variable to itself and then includes the ‘therm@Heat’ or whatever that thermostatMode value is.

Now, my question here is using $context to grab the value of a device that has special charcters in the name. All my devices within Samsung Smartthing environment are named so I can order/group them by type. I have a little OCD and all about structure and what works for me.

So example, all my smart plugs are labeled as: (PLG:LL.t) description of plug

  • :LL. represents the location short code (2 digits) ; sh is ‘shop’, sd is ‘shed’, lr is ‘living room’ and so on
  • .t represents physical device of virtual ; I use virtual plug/switches/contact sensors for different things when automating my gate on west entrance driveway, flags to turn notification on/off for various events, etc

So with the naming structure in smartthings done the way I’ve done it, when I try utilizing the $context, I’m getting some generic errors that appear to relate to the context statement. My guess is the . in the name or even the ()?

I’m trying the following and getting the ‘Error parsing’ error:
$context.(PLG:sh.p) overhead blower.switch

Is there any trick to encapsulate the device name so it’ll parse properly? Perhaps $context.[(PLG:sh.p) ]overhead blower.switch or something similar? I know in WebCore, you have to place the device name within the and it has always worked out for me.

Any help to pull the value of a device’s property within the expression would be very helpful! Thanks!


So attempted to encapsulate with {, [, etc but no go. Not sure what else to try or if this is going to even be possible.

The only other thing I can do is set ANOTHER variable to the device’s value using the ‘Action - Set Variable’ and select ‘Device Attribute’ but I feel like this entire thing is getting longer and longer to do some simple things. With WebCore, I was able to do it all in one line of code. I’m trying to get away from having to do a bunch of these ‘Action’ blocks. I was hoping one Action block to set the variable to what I want with the values I need from the devices.

I’ll hold off to see if anyone has any tricks to pull the device’s attribute value through the expression with the naming I have.

1 Like

Hi Kevin - you can find the high level documentation on Context Variables on the following page:

I would note that the documentation was written around using context variables directly through the Variable Picker (eg. in a condition or inserting it into a notification), so each of the full $ names aren’t necessarily documented there.

You can temporarily create a condition block and as you pick an option from the picker, you’ll be able to see the final variable name in the condition and you can use that in your expression. I’ve made note of the feedback though as the documentation should probably contain that information now that you can freeform enter it into an expression.

It sounds like you’re trying to get an arbitrary device’s state to use in an expression. As briefly mentioned in our other discussion, you would need to temporarily store the device state into a variable to use it in a condition. While you can access context variables about the event that triggered the rule to run (eg. $context.event.value) or reference global variables (eg. $MyTemperature), you can’t access arbitrary device state directly in an expression.

@Jeff_L recently posted a similar discussion with a similar interest. As I mentioned over there, the best way to get these kind of enhancements in place is to create a feature request and get it upvoted. Feature requests are a key part of how we prioritize what we work on and with other WebCoRE users switching over, I suspect you could probably get some votes on it.

I’ll post an update as to the ‘why’ of expressions not currently being able to reference device state directly in Jeff’s thread if you’re looking for some insight if you want to draft a feature request.

I ended up storing the values in a Temp1 and Temp2 to be used. A bit of a pain and made the entire rule very large with a lot of these variable updates. But bottom line, it works!

I’m working through an issue with the val1 = val2 ? true : false with characters instead of a number and having some issues. I think this is the last road block I’m working on now. going to re-read some of the notes you had in the other thread. Appreciate the help!

If it’s comparing two strings, use equalText("string1", "string2")

equalText(val1, val2)

If it’s numbers, make sure to use a double equals (a single equals is an assignment operator).

val1 == val2

I would also note that the result of equalText(a,b) or a == b is a boolean. So you don’t need the ternary operator unless you wanted to return something different or perform a different action in the condition ? trueOperation : falseOperation paths