So the idea behind this is to replace my current alarm system written in python.
The system has 3 main functions.
Arm / Disarm - One multiple or all zones
Status - One multiple or all zones
Trigger
Each action receives a mqtt message and the reaction would be a mqtt messages.
The system gets inputs from several devices, these are received in form of mqtt messages.
Most messages would look like this
Topic
"iincoming/alarm/zone/triggered/01" where all characters after the last /. would indicate the zone.
The output messages I wanted to easily be able to change hence the use of who context do do this.
The system also needed to keep track of various properties of a zone as it could potential have different reactions or output messages.
Since writing the original post I managed to get the system up and running.
Even though this works I don't know if any fundamental mistakes has been made.
Here is the new code
Arm Function Node
let var_obj_message_templates = flow.get("alarm_messages.alarm.arm");
let var_str_alarm_zones = msg.topic.substring(msg.topic.lastIndexOf("/") + 1);
if (var_str_alarm_zones == "00") {
var_str_alarm_zones = Object.keys(flow.get("alarm_zones.alarm.zones")).toString();
}
let var_arr_alarm_zones = var_str_alarm_zones.split(",");
for (let var_str_alarm_zone of var_arr_alarm_zones) {
let var_obj_alarm_zone = flow.get("alarm_zones.alarm.zones." + var_str_alarm_zone);
if (msg.payload == "arm" || msg.payload == "disarm") {
var_obj_alarm_zone.state = msg.payload + "ed";
var_obj_alarm_zone.state_at = "x x x x x";
}
}
if (msg.topic.indexOf("silent") != -1) {
var_obj_message_templates = flow.get("alarm_messages.alarm.arm.silent");
} else {
var_obj_message_templates = flow.get("alarm_messages.alarm.arm.normal");;
}
for (let var_str_alarm_zone of var_arr_alarm_zones) {
let var_obj_alarm_zone = flow.get("alarm_zones.alarm.zones." + var_str_alarm_zone);
for (let var_obj_message_template of Object.values(var_obj_message_templates)) {
let var_obj_message = Object.assign({}, var_obj_message_template);
for (let [var_str_key, var_str_value] of Object.entries(var_obj_alarm_zone)) {
var_obj_message.topic = var_obj_message.topic.replace("{" + var_str_key + "}", var_str_value);
var_obj_message.payload = var_obj_message.payload.replace("{" + var_str_key + "}", var_str_value);
}
node.send(var_obj_message)
}
}
node.done;
Trigger Function Node
let var_obj_message_templates = flow.get("alarm_messages.alarm.triggered");
let var_str_alarm_zone = msg.topic.substring(msg.topic.lastIndexOf("/") + 1);
let var_obj_alarm_zone = flow.get("alarm_zones.alarm.zones." + var_str_alarm_zone);;
if (var_obj_alarm_zone.state == "armed") {
var_obj_alarm_zone.triggered = "triggered";
} else {
var_obj_alarm_zone.triggered = "dtriggered";
}
var_obj_alarm_zone.triggered_at = "x x x x x";
if (var_obj_alarm_zone.state == "armed") {
var_obj_message_templates = flow.get("alarm_messages.alarm.triggered.armed");
} else {
var_obj_message_templates = flow.get("alarm_messages.alarm.triggered.disarmed");;
}
for (let var_obj_message_template of Object.values(var_obj_message_templates)) {
let var_obj_message = Object.assign({}, var_obj_message_template);
if (var_obj_message.payload.indexOf("buz,{zone},0.1") != -1) {
if (flow.get("alarm_config.alarm.beep_zones.beep_zones") == "off") {
var_obj_message.payload = var_obj_message.payload.replace("{zone}", "1");
}
}
for (let [var_str_key, var_str_value] of Object.entries(var_obj_alarm_zone)) {
var_obj_message.topic = var_obj_message.topic.replace("{" + var_str_key + "}", var_str_value);
var_obj_message.payload = var_obj_message.payload.replace("{" + var_str_key + "}", var_str_value);
}
node.send(var_obj_message)
}
node.done;
State Function Node
let var_obj_message_templates = flow.get("alarm_messages.alarm.state");
let var_str_alarm_zones = msg.topic.substring(msg.topic.lastIndexOf("/") + 1);
if (var_str_alarm_zones == "00") {
var_str_alarm_zones = Object.keys(flow.get("alarm_zones.alarm.zones")).toString();
}
let var_arr_alarm_zones = var_str_alarm_zones.split(",");
for (let var_str_alarm_zone of var_arr_alarm_zones) {
let var_obj_alarm_zone = flow.get("alarm_zones.alarm.zones." + var_str_alarm_zone);
for (let var_obj_message_template of Object.values(var_obj_message_templates)){
let var_obj_message = Object.assign({}, var_obj_message_template);
for (let [var_str_key, var_str_value] of Object.entries(var_obj_alarm_zone)){
var_obj_message.topic = var_obj_message.topic.replace("{" + var_str_key + "}", var_str_value);
var_obj_message.payload = var_obj_message.payload.replace("{" + var_str_key + "}", var_str_value);
}
node.send(var_obj_message)
}
}
node.done;
And here is the flow context data
{"alarm":{"beep_zones":{"message_01":{"topic":"feeds/thing-x/process-alarm-x/outgoing/alarm/beep_zones","payload":"{beep_zones}","retain":true}},"arm":{"silent":{"message_01":{"topic":"feeds/thing-x/process-alarm-x/outgoing/alarm/zone/state/{zone}","payload":"{state}","retain":true},"message_02":{"topic":"feeds/thing-x/process-alarm-x/outgoing/alarm/zone/triggered/{zone}","payload":"{state}","retain":true},"message_03":{"topic":"feeds/thing-x/process-telegram-x/incoming/wcx_io_alarm_status_group","payload":"alarm - zone - {zone} - {state}","retain":false}},"normal":{"message_01":{"topic":"feeds/thing-x/process-alarm-x/outgoing/alarm/zone/state/{zone}","payload":"{state}","retain":true},"message_02":{"topic":"feeds/thing-x/process-alarm-x/outgoing/alarm/zone/triggered/{zone}","payload":"{state}","retain":true},"message_03":{"topic":"feeds/thing-x/process-telegram-x/incoming/wcx_io_alarm_status_group","payload":"alarm - zone - {zone} - {state}","retain":false},"message_04":{"topic":"feeds/thing-x/process-telegram-x/incoming/wcx_io_alarm_group","payload":"alarm - zone - {zone} - {state}","retain":""},"message_05":{"topic":"feeds/thing-x/process-clock-x/incoming/clock/message/scroll","payload":"{\"message\" : \"alarm zone - {zone} - {state}\", \"delay\" : \"0.1\", \"repeat\" : \"1\"}","retain":false}}},"state":{"message_01":{"topic":"feeds/thing-x/process-alarm-x/outgoing/alarm/zone/state/{zone}","payload":"{state}","retain":true},"message_02":{"topic":"feeds/thing-x/process-alarm-x/outgoing/alarm/zone/triggered/{zone}","payload":"{state}","retain":true}},"triggered":{"armed":{"message_01":{"topic":"feeds/thing-x/process-alarm-x/outgoing/alarm/zone/triggered/{zone}","payload":"{triggered}","retain":true},"message_02":{"topic":"feeds/thing-x/process-alarm-x/outgoing/alarm/zone/triggered/timestamp/{zone}","payload":"{triggered_at}","retain":true},"message_03":{"topic":"feeds/thing-x/process-telegram-x/incoming/wcx_io_alarm_group","payload":"alarm zone - {zone} - {triggered}","retain":false},"message_04":{"topic":"feeds/thing-x/process-clock-x/incoming/clock/message/scroll","payload":"{\"message\" : \"alarm zone - {zone} - {triggered}\", \"delay\" : \"0.1\", \"repeat\" : \"1\"}","retain":false},"message_05":{"topic":"feeds/thing-x/process-alarm-x/incoming/io/switch/09","payload":"buz,{zone},0.1","retain":false}},"disarmed":{"message_01":{"topic":"feeds/thing-x/process-alarm-x/outgoing/alarm/zone/triggered/{zone}","payload":"{triggered}","retain":true},"message_02":{"topic":"feeds/thing-x/process-alarm-x/outgoing/alarm/zone/triggered/timestamp/{zone}","payload":"{triggered_at}","retain":true}}}}}
{"alarm":{"zones":{"10":{"zone":"10","state":"armed","state_at":"x x x x x","triggered":"","triggered_at":""},"00":{"zone":"00","state":"armed","state_at":"x x x x x","triggered":"","triggered_at":""},"01":{"zone":"01","state":"armed","state_at":"x x x x x","triggered":"","triggered_at":""},"02":{"zone":"02","state":"armed","state_at":"x x x x x","triggered":"","triggered_at":""},"03":{"zone":"03","state":"armed","state_at":"x x x x x","triggered":"","triggered_at":""},"04":{"zone":"04","state":"armed","state_at":"x x x x x","triggered":"","triggered_at":""},"05":{"zone":"05","state":"armed","state_at":"x x x x x","triggered":"","triggered_at":""},"07":{"zone":"07","state":"armed","state_at":"x x x x x","triggered":"","triggered_at":""}}}}