I've been building a monitoring system that checks whether a device is alarmed or not, and if so, sends notifications to relevant users based on how long a device is in a particular state. Here's an example of the criteria I'm building around:1.) If a device has previously operated in a non-alarmed state for more than an hour previouslya.) If the device has been alarmed for more than 15 minutes, notify level 1 usersb) If the device has been alarmed for more than 24 hours, notify level 2 users2.) If a device reached a level one alarm statea.) If a device has currently been operating in a non-alarmed state for more than an hour, notify all relevant usersTo build this system I've had to construct a lot of nested conditional logic and track state changes in a cache I set up. My server continuously gets new data on a group of devices and runs their states through alarm logic. The problem is that this system is very brittle and any changes I make to the logic for one state influence the others because they are intertwined. E.g.:var deviceState = {...}; // cachedDataif (deviceState.hasOperatedWithoutAlarmForHour === true) {    if (currentStatus === 'ALARMED') {        if (deviceState.previousStatus !== 'ALARMED') {            deviceState.hasSentLevelOneNotice = false;            deviceState.hasSentLevelTwoNotice = false;            deviceState.alarmStateStartTime = new Date();        }        else if (            deviceState.hasSentLevelOneNotice === false            && (currentTime - deviceState.alarmStateStartTime) > 15_MINUTES_CONSTANT)        ) {            sendLevelOneNotice();            deviceState.hasSentLevelOneNotice = true;        }        ...    }}else {    if (deviceState.previousStatus === 'ALARMED') {        deviceState.hasOperatedWithoutAlarmForHour = false;        deviceState.noAlarmStartTime = new Date();        deviceState.hasSentAllClearNotice = false;    }    else {        if (            deviceState.hasSentAllClearNotice === false            && (currentTime - deviceState.noAlarmStartTime) > ONE_HOUR_CONSTANT            && deviceState.hasSentLevelOneNotice === true        ) {            sendAllClearNotice();            deviceState.hasSentAllClearNotice = true;        }        ...    }}Is there a better way to construct a state-based monitoring system that tracks time duration in a given state?
Tracking state changes based on duration of time in state
javascript;node.js;monitoring
I might design this system to take advantage of some sort of persistent state, seeing as how it operates over the course of many hours, if not days. This would allow for exit and re-entry of the application without effecting it's course of action.To enable that functionality the following may suffice:Device Status (as of time of check), along with UTC timestamp.Action Taken (user notified, along with reason: error / recovery), along with UTC timestampI would then write multiple queries to determine what future action I should take for any given device, take that action, and log the action accordingly. Logging the action would change the result of the query and indicate that the action is no longer required.Your queries and determined action would pair similar to the following:To determine required notifications for level 1 users find all devices where the latest alarm time is greater than the latest level 1 notification time and greater than the latest non-alarmed time + 15 minutes.To determine required notifications for level 2 users find all devices where the latest alarm time is greater than the latest level 2 notification time and greater than the latest non-alarmed time + 24 hours.To determine required notifications for non-alarm status find all devices where the latest non-alarm time is greater than the latest non-alarm notification + 1 hourSend said notifications, and log accordingly. Repeat as often as is useful per service level agreements.