Switching based on objects

Trying to make a switch node to change the flow based on the ON or OFF status. I have tried this so far and none of it works as expected. No matter whats in the object ON or OFF the flow never changes

[
    {
        "id": "69610940.e72308",
        "type": "function",
        "z": "d4c8cc0f.e4bd8",
        "name": "Light function",
        "func": "msg.topic = \"SELECT `Light` FROM `kitchen`\";\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 250,
        "y": 240,
        "wires": [
            [
                "7f112a92.583094"
            ]
        ]
    },
    {
        "id": "1bc8e6d2.9e1259",
        "type": "inject",
        "z": "d4c8cc0f.e4bd8",
        "name": "Get data",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "1",
        "crontab": "",
        "once": true,
        "onceDelay": "",
        "topic": "",
        "payload": "1",
        "payloadType": "flow",
        "x": 100,
        "y": 240,
        "wires": [
            [
                "69610940.e72308"
            ]
        ]
    },
    {
        "id": "7f112a92.583094",
        "type": "mysql",
        "z": "d4c8cc0f.e4bd8",
        "mydb": "",
        "name": "",
        "x": 390,
        "y": 240,
        "wires": [
            [
                "d4b0a31574a96d99",
                "fc2983a8145b81aa",
                "91e923b0b3f52f89",
                "a809e00408fa6ce7"
            ]
        ]
    },
    {
        "id": "915345e5376912a7",
        "type": "debug",
        "z": "d4c8cc0f.e4bd8",
        "name": "debug 24",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 980,
        "y": 240,
        "wires": []
    },
    {
        "id": "449793f2487ec557",
        "type": "debug",
        "z": "d4c8cc0f.e4bd8",
        "name": "debug 26",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 980,
        "y": 280,
        "wires": []
    },
    {
        "id": "f2f1214512ef129f",
        "type": "debug",
        "z": "d4c8cc0f.e4bd8",
        "name": "debug 27",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 980,
        "y": 320,
        "wires": []
    },
    {
        "id": "0e15e53ced39b24a",
        "type": "debug",
        "z": "d4c8cc0f.e4bd8",
        "name": "debug 28",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 980,
        "y": 160,
        "wires": []
    },
    {
        "id": "1a85f9bd1f0c2a20",
        "type": "debug",
        "z": "d4c8cc0f.e4bd8",
        "name": "debug 29",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 980,
        "y": 200,
        "wires": []
    },
    {
        "id": "fc2983a8145b81aa",
        "type": "switch",
        "z": "d4c8cc0f.e4bd8",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "cont",
                "v": "ON",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "ON",
                "vt": "msg"
            },
            {
                "t": "neq",
                "v": "ON",
                "vt": "msg"
            },
            {
                "t": "hask",
                "v": "ON",
                "vt": "msg"
            },
            {
                "t": "regex",
                "v": "ON",
                "vt": "msg",
                "case": false
            },
            {
                "t": "eq",
                "v": "ON",
                "vt": "flow"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 6,
        "x": 710,
        "y": 200,
        "wires": [
            [
                "0e15e53ced39b24a"
            ],
            [
                "1a85f9bd1f0c2a20"
            ],
            [
                "915345e5376912a7"
            ],
            [
                "449793f2487ec557"
            ],
            [
                "f2f1214512ef129f"
            ],
            [
                "40a1f92ad477f696"
            ]
        ]
    },
    {
        "id": "d4b0a31574a96d99",
        "type": "function",
        "z": "d4c8cc0f.e4bd8",
        "name": "function 107",
        "func": "var element = msg.payload[0]; // Access the third element of the array\nreturn { payload: element };",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 790,
        "y": 440,
        "wires": [
            [
                "b1f00a44e198c692"
            ]
        ]
    },
    {
        "id": "40a1f92ad477f696",
        "type": "debug",
        "z": "d4c8cc0f.e4bd8",
        "name": "debug 30",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 980,
        "y": 360,
        "wires": []
    },
    {
        "id": "91e923b0b3f52f89",
        "type": "debug",
        "z": "d4c8cc0f.e4bd8",
        "name": "debug 31",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 980,
        "y": 400,
        "wires": []
    },
    {
        "id": "b1f00a44e198c692",
        "type": "debug",
        "z": "d4c8cc0f.e4bd8",
        "name": "debug 32",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 980,
        "y": 440,
        "wires": []
    },
    {
        "id": "a809e00408fa6ce7",
        "type": "change",
        "z": "d4c8cc0f.e4bd8",
        "name": "",
        "rules": [
            {
                "t": "delete",
                "p": "topic",
                "pt": "msg"
            },
            {
                "t": "move",
                "p": "payload.Light",
                "pt": "msg",
                "to": "Light",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 600,
        "y": 620,
        "wires": [
            [
                "96df872ebaf38e1c",
                "b6f09488738cbf8f",
                "3c165f43279c627d"
            ]
        ]
    },
    {
        "id": "21093b6be5c5e5d4",
        "type": "debug",
        "z": "d4c8cc0f.e4bd8",
        "name": "debug 33",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 980,
        "y": 560,
        "wires": []
    },
    {
        "id": "96df872ebaf38e1c",
        "type": "switch",
        "z": "d4c8cc0f.e4bd8",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "Light: 'OFF'",
                "vt": "env"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 830,
        "y": 560,
        "wires": [
            [
                "21093b6be5c5e5d4"
            ]
        ]
    },
    {
        "id": "b6f09488738cbf8f",
        "type": "switch",
        "z": "d4c8cc0f.e4bd8",
        "name": "",
        "property": "Light",
        "propertyType": "msg",
        "rules": [
            {
                "t": "hask",
                "v": "OFF",
                "vt": "msg"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 830,
        "y": 600,
        "wires": [
            [
                "9d76202fb3a0e1be"
            ]
        ]
    },
    {
        "id": "9d76202fb3a0e1be",
        "type": "debug",
        "z": "d4c8cc0f.e4bd8",
        "name": "debug 34",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 980,
        "y": 600,
        "wires": []
    },
    {
        "id": "3c165f43279c627d",
        "type": "debug",
        "z": "d4c8cc0f.e4bd8",
        "name": "debug 35",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 980,
        "y": 640,
        "wires": []
    }
]

Sorry I know i uploaded the code wrong. It's a learning process

Edit the post and put a line of 3 back ticks ( ` ) above the code, another below the code.

Thanks. for the help

It would help if you put a debug node (set to display the complete msg object) on the output of the mysql node so we could see what the actual data is being passed to the change: 2 rules node.

Why do you use two switch nodes after the change: 2 rules` node? You can test multiple conditions in the switch node,

I figured it out. I knew it was something simple, its all cleaned up and working now.

OK so you have a select statement SELECT light FROM kitchen.

What does your Debug 31 node show the query returns?
As long as the query is valid, msg.payload will be either an empty array or an array of objects, each having the key "light" and a value.
This is vital to understand - your struggles with msg.ON, flow.ON, msg.payload.Light etc are all because you have not yet grasped this.

In your switch tests you are looking at msg.payload when you should probably be looking at msg.payload[0].light.

I had the property on the switch node wrong so i was just trying any way possible to make it work. I finally seen it hours later. Thanks for your help

Yes this was the issue.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.