Expression Writing

Hey Braintrust.

Having difficulties getting my head around more complex expression writing, if else, operators, et cetera. Watched the HE tutorial and can do the basics at this point.

Want to up my game. Anyone have any suggestions on quality tutorials tailored for HE?

TIA.
Dave

1 Like

I'm taking guesses at the context of your question, but do you mean in Rule Machine? If so, is the issue with how to create these expressions in the UI or is it with what they mean/how to use them?

If it's more of the first thing, I'm not aware of any specific tutorials, though here is an example that at least shows how to create a somewhat "complex" expression by combining two conditions: Rule Machine 2: Advance Conditional Rules - YouTube. It's a couple years old at this point and from Rule 4.x, but this particular part of the UI is mostly the same (the action editing table is a lot different--use the "+" button at the bottom left to add actions now, but the rest should be similar). The use of "expression" vs "condition" was also mostly clarified, where "expression" is now used to mean either a single condition or multiple conditions combined in whatever way.

If you're talking more about just how to use them, they work like boolean values do in boolean algebra (not as scary as it sounds, at least not for these purposes...) or similar boolean values and operators in most programing languages. If you have two conditions, with one evaluating to false and one evaluating to true, the outcomes with the basic RM operators will be like:

  • T AND F = FALSE
  • T OR F = TRUE

If both true, we get:

  • T AND T = TRUE
  • T OR T = TRUE

You can probably imagine the rest; if not, search for "truth tables" and you'll find more than you wanted to know.

The NOT operator just returns the opposite value:

  • NOT T = FALSE
  • NOT F = TRUE

Parentheses allow you to group expressions, with innermost ones evaluated first, same as you may have learned with "normal" math. Expressions in Hubitat (not always in all programming languages, so this can be different) are evaluated left to right with all "binary" operators (AND, OR, etc.) having equal precedence, not including NOT (applies to condition/subexpression directly to its right) or, again, where parentheses force a different order.

By default, if "Display current values" is selected in RM, the current value of each condition will be displayed in parentheses after, e.g. "Front door is open(F)". You'll also see the entire evaluation of the expression in square brackets at the end, e.g., 'Front door is open(F) AND Front door is unlocked(T) [FALSE]". These can be helpful if you aren't sure how your logic combines.

1 Like

You’re on point, I am talking expressions in terms rulemachine. I am really trying to tease out syntax, use of parentheticals, sub expressions, available commands, et cetera.

A hypothetical would be, and just pulling this out of thin air, exterior avg temp <= 50 degrees, presence is true, interior temp <= 70, turn on fire place. Then once interior temp >= 73 shut off. Run rule constantly when mode is fall and day.

I have established groups for interior, exterior, temp and lux leveraging the simple groups app. These populate various virtual device attributes. Would love to start applying these to rules within RM based upon compound expressions.

Also of value would be some reference of commands that could be leveraged on various devices types and available drivers. Ie how to send DIM and RGB commands to a hue bridged device. I have seen an app somewhere that pulls all possible attributes from a device. How nice it would be to know the commands it could interpret.

Lastly, great springboard response. Thank you, that info helps already.

An app I wrote to poke into things:
https://raw.githubusercontent.com/thebearmay/hubitat/main/apps/deviceCharacteristics.groovy

Should let you see the commands supported with the parameter types needed, as well as the attributes and more.

1 Like

Rule Machine, and really most apps, are designed to make this easier than needing to know the exact commands to send (though you can see those, and test them, on their device detail page if you want).

For example, in Rule Machine, to set the color of any bulb (Hue or not; one role of drivers is to abstract as much of this detail as possible away from you--and any app that uses the device), you can use the Set Dimmers and Bulbs > Set Color action, or really anything similar that also does what you want. Many RM actions have a clear correspondence with a particular device command (this one being setColor(), though some are more complicated features provided by the app, like "Toggle" or the "...over time" actions).

For some of your other questions, it sounds like they are more than just expressions or conditional actions. I would familiarize yourself with rule execution first: in general, a trigger event makes rule actions run (unless a required expression, optional, is false)--from the top, canceling waits but not delays unless you take additional actions. Conditions actions (IF...) are evaluated when they are reached, then any matching blocks run. No event subsections are created to wait for anything to become true if it isn't--unless you add something to the actions yourself that does so, like "Wait for (expression)".

The Rule 5.1 docs are a good place to start if you haven't looked at them already: Rule 5.1 | Hubitat Documentation

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.