Real-Time Expressions: Display "On" if Any Value is "On"


Overview

Display "On" if any value in multiple data streams is "On". For example, given multiple data streams, each providing the status of a light being on or off, create a new data stream that is "On" if any light is on and "Off" if all lights are off. This operation utilizes the concatenate (&), partial match (~), map (:), and union (|) operators. 

Syntax


Concatenate multiple data streams together,
search for a partial match of the desired value,
if true, display a desired string or emoji,
if false, display another desired string or emoji

=((([data stream1] & [data stream2]) ~ "<search string>") : <display string1>) | (([data stream1] | [data stream2]) : <display string2>)

Example1

There are two data streams, light1 and light2, displaying the status of a light, either "On" or "Off". We want to display "ON" if either data stream is "On". We want to display "OFF" if both data streams are "Off". We will build our real-time expression one step at a time.

Step 1: Concatenate (&) the two data streams together
=[light1] & [light2] 
The result will be one of four values: "OnOff", "OffOn", "OnOn", "OffOff".

Step 2: Search for a partial match (~) of "On"
=([light1] & [light2]) ~ "On"
If "On" is anywhere in the resulting four values above, the concatenated string will be returned ("OnOff", "OffOn", "OnOn").

Step 3: Replace any successful partial match with a new value using the map (:) operator
=((([light1] & [light2]) ~ "On") : "ON")
"OnOff", "OffOn", "OnOn" will all be replaced by "ON" using the map (:) operator. Instead of "ON", an emoji such as the light bulb or another string could be used.

Step 4: If no partial match is found, return the "Off" value using the union (|) operator
=((([light1] & [light2]) ~ "On") : "ON") | ([light1] | [light2])
The union (|) operator gives the left operand a higher priority. That means that if the search for "On" is true, the value returned will be "ON". Else, the value of the next operand to the right will be returned. In this case, "Off" will be returned from either light1 or light2.

Step 5: Replace the "Off" value with a desired string or emoji using the map (:) operator
=((([light1] & [light2]) ~ "On") : "ON") | (([light1] | [light2]) : "OFF")
Instead of "OFF", an emoji such as a black dash or another string could be used.

Example2

There are two data streams, motion1 and motion2, displaying the status of a motion sensor, either "active" or "inactive". We want to display "ACTIVE" if either data stream is "active". We want to display "INACTIVE" if both data streams are "inactive".

This example is similar to the example above except you cannot just search for a partial match of "active" because "active" is a partial match of "inactive".

One solution is to append an additional character to each data stream such as '-', then search for a partial match on "-active".

=((('-' & [motion1] & '-' & [motion2]) ~ "-active") : "ACTIVE") | (([motion1] | [motion2]) : "INACTIVE")

Another solution is to look for "not a partial match (!~)" of "inactiveinactive":

=((([motion1] & [motion2]) !~ "inactiveinactive") : "ACTIVE") | (([motion1] | [motion2]) : "INACTIVE")

Feedback and Knowledge Base