Skip to main content

State Variable

We want to create a machine state as if we had a CNC that sends us a "1" every time a part is created, otherwise the input signal returns to "0".

Create a state variable

To define your state, we need to create a boolean variable that defines if your state is ON (true) or OFF (false). For this example, we will assume that if my CNC does not sends a "1" for 2 minutes, we will consider it OFF.

Setup

Create a variable named CNC_received_signal_ts with 0 as the expression to save the time when you received your last signal.

note

ts in the variable name means "timestamp". Check Tips and Tricks to see what a timestamp is and how to use NOW.

Record when you receive a signal

Go to the rules engine and create a rule with a Value trigger to trigger when we receive a signal "1" from the CNC. A Value Change is used here because we know that our variable will drop to "0" right after.

Machine State value trigger

You action will be a Variable action. The action sets the CNC_received_signal_ts to NOW. This records when you received your signal.

Machine State variable action

Create the state variable

We have now everything we need to create a state variable that is true or false if the last signal received is before or after 2 minutes ago.

Go to Variables and create a variable named CNC_state.
The expression will be ($NOW - $CNC_received_signal_ts) < 120000.

What we do here is that we substract the last time we received a signal from the current date NOW. If this value is under 120 000ms (2 minutes) this means that our machine is ON.

tip

It is best practice that your delay (120 000 ms) is stored in a variable of it's own, it's easier to know what it is afterwards.

Now that we have our variable, we still have to create our corresponding machine state events.