[Guide] React Concept: The Event Loop
So I wanted to go through a concept that we have been doing with Device Event Triggers a bit. This concept is the "Event Loop." So to start lets consider a regular system without any Dash Apps. In this scenario you have a simple system which is generally made to respond to a user interacting with the system. When your users press a button, things will happen - simple right? Well that just isn't automation!
URC then introduced the "Device Events" and "TCL Commands" features which allow modules to extend the functionality of the platform. This is when the first steps of Home Automation really started with URC. However, the system is still just standing idly by, waiting for something to occur. It isn't proactively monitoring the home and automatically responding to almost any piece of information available to us unless those events happen to have Device Events - and in those cases it can't always provide the type of functionality you might be looking for.
Introducing: Event Loops
Now lets imagine an Automation System that can proactively monitor the home, react to almost any event, and be customized to your hearts content. An Event Loop is doing just that, and at the end of the day all it really is doing is looping macros at set intervals, checking our variables and/or interacting with installed modules and conducting some sort of functionality based upon what it finds.
Event Loops are actually a fairly simple concept both in the idea behind it and how it's accomplished within our programming.
Lets take a look at how these are done!
- Dash React always starts at the Automation Macros Section of Accelerator. This is where you are programming your reactions to variable events. Let's create a new Dash React Device Event
- For an Event Loop to occur we need to have a macro which is running at an interval. With Dash React we accomplish this with the "While Variable ..." Event. With this Event, you will be creating a macro that will run so-long as the parameters you have entered remain true. This gives us a lot of flexibility to create interesting ways for our system to react to events.
Variable Name: When we create events, we are actually simultaneously creating Variables. When Dash React first starts up, it will read through every event you have programmed. It will create any Variables we have created events for and allow our TCL Commands to run their commands on them. We will go over best practices for variable naming & when to create different variables in other tutorials, for now we can just use something like SystemMonitor
Remember: Creating Events is actually creating our variables!
Operator: The Operator Dropdown allows us to decide how we want React to match against the current value of the Variable. Each time the variable changes, React will trigger any events that currently match the event we have created. While the others provide some really cool functionality, for the purposes of this tutorial, we will just choose the simple "Equals" option. This means the value of the variable will have to be exactly what we type into the value field or else this Event Macro will never be fired.
Value: We now need to enter what we want the value to be for this event to fire. So-long as the value matches what we enter, in our case the macro will fire at the given interval (which we will configure next). Lets say that whenever the variable is equal to "Active" it will be firing our event.
Every (Seconds) Now we define at what interval we want our macro to fire. Obviously the faster we fire our Event Loop, the faster our system will respond to events. However, Event Loops should often be used for monitoring things at a higher-level. Depending on your needs, some go as low as every 1 minute for their Event Loops - but it is best to have your loops every 5-10 minutes to be safe. Lets do every 10 minutes (or 600 Seconds).
Event Name: With React it is actually fairly important to be able to read your events and know immediately what every event is doing. This is important for quickly troubleshooting your events and maintaining the system as it grows. You are welcome to adopt your own methods for doing this, in our case we will name this event SystemMonitor While = Active > 10 Minutes - this should tell us right away when we can expect this event to fire (you will also notice that we prefix each event with a letter telling us what Module the event is for. A for Dash Access and R for Dash React).
- Alright, so we have our Device Event setup and ready to start looping just as soon as our "SystemMonitor" variable is set to "Active." Next we will go over how we will handle that! First, lets take a look at the final result of our Device Event!
Setting Our System Loop to Start
We've created our reaction, but now we need to create the action which will make our Reaction Event. In this case, we want our Event to almost always be firing so we need to set our variable as soon as the system starts up. We have System Startup Events available to us within the "Dash Event Triggers" Dash App so we will use that to set our variables value.
If you aren't familiar with how this Event works, it will schedule a macro to occur about 5 minutes after the initial startup of the system. This time is used to allow all other Dash Apps & URC Hardware time to boot up and be ready to receive commands from our macro.
Setting our Variables Value
We can set and modify our variables values using URC's TCL Command features. With Dash React, we have many options to choose from. Start by opening the TCL Commands window and selecting Dash React from the dropdown menu.
We have a lot to choose from with Dash React, for this tutorial we will only worry about the simple "Set Variable (String)" option.
Variable Name: For Variable name we need to use the exact name of the Variable we created while setting up our Device Event. Enter "SystemMonitor" as that is the Variable we created earlier.
Value: What we enter here is what the variable is going to change to. We are going to enter "Active" as that is how we programmed our Device Event.
That's It! We now have an Event Loop and we can start programming some fun automation and make our systems smarter!last edited by Braden Napier
Braden R. Napier