Stop Flow based on input boolean value

I just started experimenting with node-red and am trying to convert existing yaml automations one at a time. I’m having trouble figuring out how to disable the flow of a node based on an input boolean. I would like to mimic the functionality I currently have where I can disable an automation manually by toggling it.

I figured to do this node-red I’d need to create an input boolean then in the flow check if the input boolean is on or off. If it’s on, continue with the flow, if it’s off, halt. It seems pretty straightforward but I might not be thinking in node-red correctly just yet.

The behavior that I am seeing is that it works if I don’t have the Check State node. If the Check State node is connected and is enabled I get a javascript error "TypeError: Cannot read property 'state' of undefined". My guess is that the Check State node does not pass along the msg payload from the ‘Kodi Downstairs State Change’ node so the payload is undefined since the Check State node isn’t passing it any data. Am I doing this correctly and just missing a step, or is there a better way to accomplish this task?

Here is what the flow looks like visually and with the json:

[
    {
        "id": "c3dd41f8.885738",
        "type": "tab",
        "label": "Test",
        "disabled": false,
        "info": ""
    },
    {
        "id": "fe54ecce.929ed8",
        "type": "server-state-changed",
        "z": "c3dd41f8.885738",
        "name": "Kodi Downstairs State Change",
        "server": "b454cc2c.3067b",
        "entityidfilter": "media_player.kodi_downstairs",
        "entityidfiltertype": "exact",
        "outputinitially": false,
        "state_type": "str",
        "haltifstate": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "outputs": 1,
        "output_only_on_state_change": true,
        "x": 155,
        "y": 325,
        "wires": [
            [
                "2c52afbd.606418"
            ]
        ]
    },
    {
        "id": "acac89e1.727a98",
        "type": "function",
        "z": "c3dd41f8.885738",
        "name": "Playing/Paused/Stopped ?",
        "func": "var newState = msg.data.new_state.state;\nvar oldState = msg.data.old_state.state;\nif (oldState != \"playing\" && newState == \"playing\") {\n    return [ msg, null, null ];\n}\nelse if (oldState == \"playing\" && newState == \"paused\") {\n    return [ null, msg, null ];\n}\nelse { \n    return [ null, null, msg ]; \n}\n",
        "outputs": 3,
        "noerr": 0,
        "x": 638,
        "y": 318,
        "wires": [
            [
                "9622ef20.a12f4"
            ],
            [
                "716c2f94.c8085"
            ],
            [
                "d977e297.7f61d8"
            ]
        ]
    },
    {
        "id": "9622ef20.a12f4",
        "type": "api-call-service",
        "z": "c3dd41f8.885738",
        "name": "Dim Lights",
        "server": "b454cc2c.3067b",
        "service_domain": "light",
        "service": "turn_off",
        "data": "{\"entity_id\":\"light.living_room\",\"transition\":2}",
        "mergecontext": "",
        "output_location": "",
        "output_location_type": "none",
        "x": 869,
        "y": 244,
        "wires": [
            []
        ]
    },
    {
        "id": "d977e297.7f61d8",
        "type": "api-call-service",
        "z": "c3dd41f8.885738",
        "name": "Raise Lights",
        "server": "b454cc2c.3067b",
        "service_domain": "light",
        "service": "turn_on",
        "data": "{\"entity_id\":\"light.living_room\",\"transition\":2}",
        "mergecontext": "",
        "output_location": "",
        "output_location_type": "none",
        "x": 875,
        "y": 393,
        "wires": [
            []
        ]
    },
    {
        "id": "716c2f94.c8085",
        "type": "api-call-service",
        "z": "c3dd41f8.885738",
        "name": "Partial Lights",
        "server": "b454cc2c.3067b",
        "service_domain": "light",
        "service": "turn_on",
        "data": "{\"entity_id\":\"light.living_room\",\"transition\":1,\"brightness_pct\":5,\"color_name\":\"red\"}",
        "mergecontext": "",
        "output_location": "",
        "output_location_type": "none",
        "x": 868,
        "y": 317,
        "wires": [
            []
        ]
    },
    {
        "id": "2c52afbd.606418",
        "type": "api-current-state",
        "z": "c3dd41f8.885738",
        "name": "Light Dimming Enabled",
        "server": "b454cc2c.3067b",
        "outputs": 2,
        "halt_if": "off",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "override_topic": false,
        "entity_id": "input_boolean.dim_media_lights",
        "state_type": "str",
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 408,
        "y": 408,
        "wires": [
            [
                "acac89e1.727a98"
            ],
            []
        ]
    },
    {
        "id": "b454cc2c.3067b",
        "type": "server",
        "z": "",
        "name": "Home Assistant",
        "legacy": false,
        "hassio": false,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open",
        "connectionDelay": true
    }
]

Thanks in advance for any assistance!

In the “Light Dimming Enabled” node:
image

Set State Location and Entity Location to None.

In Node-Red, nodes usually modify the object named “msg”, which contains different properties according to which node created it. The node where you’re checking the status is modifying the original message, which is why the next one is failing, since it was expecting the msg created by the first node.

Setting those options to none is telling Node-Red to not modify the original message, because you don’t actually care about storing the result, just checking it. That way, the original data from Kodi Downstairs State Change won’t be changed and your flow can continue as you planned.

Hope I explained it somewhat clearly.

If it doesn’t work, please post what errors are you getting.

1 Like

Thank you, that makes sense and confirms my suspicions. I’ll give it a try tonight.

Thanks @areks, that worked perfectly.