[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.

    upload-e86a8f2b-302f-4e03-9866-b1912c857827

    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!

    upload-e449ccbe-5f64-4eca-b383-969f5ae5e06a

    • 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

    upload-06e4a0d5-2e15-4ca6-9769-e41cbb0a4e6b

    • 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!

    upload-9c7450ad-466d-4570-966e-7552cca98146

    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.

    upload-dc05cea2-957d-401b-9deb-8236e607c2a1
    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.

    upload-7297cae6-1e5a-41a4-bd9b-fcb5fe6dec4f

    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).

    upload-3230142a-d909-4c03-a76b-339a7fc3cebc

    • 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!

    upload-77da8a73-5c85-4331-88e5-bd9e07a17f9a

    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.

    upload-4e7ebdb2-89d9-4120-a857-4ab0436e9b78

    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.

    upload-2d72de19-ac40-41b9-b240-6dc26dac2768

    We have a lot to choose from with Dash React, for this tutorial we will only worry about the simple "Set Variable (String)" option.

    upload-9ad4166c-f1d5-4ef5-817e-e62d3ceb0c8b

    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.

    upload-0f7503e7-bb57-412c-8fd7-03fe65f4f2b9

    That's It! We now have an Event Loop and we can start programming some fun automation and make our systems smarter!

    --
    Braden R. Napier


  • 1
    Posts
    601
    Views
    Log in to reply

Internal error.

Oops! Looks like something went wrong!