# Advice please on best way to attack this

**URL:** <https://discourse.nodered.org/t/advice-please-on-best-way-to-attack-this/2719>\
**Category:** General\
**Created:** [31 August 2018 06:25 UTC](https://discourse.nodered.org/t/advice-please-on-best-way-to-attack-this/2719 "2018-08-31T06:25:26Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [1 September 2018 08:25 UTC](https://discourse.nodered.org/t/advice-please-on-best-way-to-attack-this/2719/13 "2018-09-01T08:25:48Z")

</div>

Hi Craig,

The approach that came to my mind is a mix of what you want to do and what Colin just suggested.

Instead of using a bunch of nested switch cases you could instead create an object that works as a table lookup. As Colin said it is good to engineer a solution that makes easier to implement any eventual change without messing up something else. It is much better to change an object than rebuilding a complex code with nested switch cases (and all those tricky breaks statements).

I understand that your RBE node will provide you with two numbers whenever there is a state change. If you combine these two numbers into a single string it could be used as a key to fetch the table lookup object. In the object, you can have a property that says what the change is about (eg. property status) and a property that is a function (therefore a method) that may be used to take actions to respond to the change.

Example of an object could be:

```auto
let def = {
    
    "176192": {
        "status": "pump turned on",
        "action": function () { node.warn("176192"); }
    },
    "176208": {
        "status": "pump turned on and boiler turned on and solar off",
        "action": function () { node.warn("176208"); }
    },
    "176240": {
        "status": "pump turned on and boiler turned on",
        "action": function () { node.warn("176240"); }
    },
    "240176": {
        "status":
            "pump turned off and boiler turned off",
        "action": function () { node.warn("240176"); }
    },
    "208192": {
        "status": "boiler turned on",
        "action": function () { node.warn("208192"); }
    }
};

```

Follows a flow that put it all together. The action I coded in the functions is only a display but you can do whatever you need, like build an MQTTT reply for instance.

```auto
[{"id":"e60c1848.624e28","type":"tab","label":"Flow 15","disabled":false,"info":""},{"id":"e4eefb9b.fe1b78","type":"inject","z":"e60c1848.624e28","name":"","topic":"","payload":"240176","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":240,"wires":[["9cc5fb51.b4d948","4643eec9.d5e6f"]]},{"id":"5eb39148.2562f","type":"function","z":"e60c1848.624e28","name":"Create Object","func":"let def = {\n \n \"176192\": {\n \"status\": \"pump turned on\",\n \"action\": function () { node.warn(\"176192\"); }\n },\n \"176208\": {\n \"status\": \"pump turned on and boiler turned on and solar off\",\n \"action\": function () { node.warn(\"176208\"); }\n },\n \"176240\": {\n \"status\": \"pump turned on and boiler turned on\",\n \"action\": function () { node.warn(\"176240\"); }\n },\n \"240176\": {\n \"status\":\n \"pump turned off and boiler turned off\",\n \"action\": function () { node.warn(\"240176\"); }\n },\n \"208192\": {\n \"status\": \"boiler turned on\",\n \"action\": function () { node.warn(\"208192\"); }\n }\n};\n\nflow.set(\"def\", def);\n\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":80,"wires":[[]]},{"id":"9cc5fb51.b4d948","type":"function","z":"e60c1848.624e28","name":"Display status change","func":"let d = flow.get(\"def\");\nnode.warn(d[msg.payload].status);\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":240,"wires":[[]]},{"id":"4643eec9.d5e6f","type":"function","z":"e60c1848.624e28","name":"Execute action","func":"let d = flow.get(\"def\");\nd[msg.payload].action();\nreturn msg;","outputs":1,"noerr":0,"x":520,"y":300,"wires":[[]]},{"id":"598bc7b9.926518","type":"inject","z":"e60c1848.624e28","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":150,"y":80,"wires":[["5eb39148.2562f"]]}]

```

---

_[View the full topic](https://discourse.nodered.org/t/advice-please-on-best-way-to-attack-this/2719)._
