Light switch with dimmer

Just getting into Node-Red and I’ve started building a dashboard.

At the moment, I have one bulb in there with an on/off switch and a slider for the brightness.

The problem is that they aren’t in sync. So if I set the brightness to zero, the switch stays on. If I set the brightness to 50% and then switch the light off, the brightness stays at 50%

Is there a way to sync the 2 entities?

This is what the flow looks like:

This is the flow:

[
    {
        "id": "ef1fe1e3.2f499",
        "type": "tab",
        "label": "Dashboard",
        "disabled": false,
        "info": ""
    },
    {
        "id": "10bf32fd.4c497d",
        "type": "ui_slider",
        "z": "ef1fe1e3.2f499",
        "name": "",
        "label": "Sofa lamp",
        "tooltip": "",
        "group": "c4803627.12f8e8",
        "order": 0,
        "width": 0,
        "height": 0,
        "passthru": true,
        "outs": "all",
        "topic": "",
        "min": 0,
        "max": "100",
        "step": "10",
        "x": 110,
        "y": 140,
        "wires": [
            [
                "cc62b54f.5269f8"
            ]
        ]
    },
    {
        "id": "cc62b54f.5269f8",
        "type": "function",
        "z": "ef1fe1e3.2f499",
        "name": "",
        "func": "var newMsg =  {  \n   payload:{  \n      \"data\":{  \n         \"brightness_pct\":msg.payload,\n         \"transition\":\"3\"\n      }\n   }\n}\nreturn newMsg;",
        "outputs": 1,
        "noerr": 0,
        "x": 270,
        "y": 140,
        "wires": [
            [
                "6a3ad024.d705c"
            ]
        ]
    },
    {
        "id": "6a3ad024.d705c",
        "type": "api-call-service",
        "z": "ef1fe1e3.2f499",
        "name": "",
        "server": "96ebf4f8.6d0e78",
        "version": 1,
        "debugenabled": false,
        "service_domain": "light",
        "service": "turn_on",
        "entityId": "light.sofa_lamp",
        "data": "",
        "dataType": "json",
        "mergecontext": "",
        "output_location": "",
        "output_location_type": "none",
        "mustacheAltTags": false,
        "x": 470,
        "y": 140,
        "wires": [
            []
        ]
    },
    {
        "id": "30cc0233.3cb30e",
        "type": "ui_switch",
        "z": "ef1fe1e3.2f499",
        "name": "",
        "label": "Sofa lamp",
        "tooltip": "",
        "group": "c4803627.12f8e8",
        "order": 1,
        "width": "2",
        "height": "2",
        "passthru": true,
        "decouple": "false",
        "topic": "",
        "style": "",
        "onvalue": "true",
        "onvalueType": "bool",
        "onicon": "fa-lightbulb-o fa-4x",
        "oncolor": "#ff9900",
        "offvalue": "false",
        "offvalueType": "bool",
        "officon": "fa-lightbulb-o fa-4x",
        "offcolor": "#ffffff",
        "x": 290,
        "y": 220,
        "wires": [
            [
                "8df17538.1785e8"
            ]
        ],
        "icon": "font-awesome/fa-lightbulb-o"
    },
    {
        "id": "8df17538.1785e8",
        "type": "api-call-service",
        "z": "ef1fe1e3.2f499",
        "name": "Sofa toggle",
        "server": "96ebf4f8.6d0e78",
        "version": 1,
        "debugenabled": false,
        "service_domain": "light",
        "service": "toggle",
        "entityId": "light.sofa_lamp",
        "data": "",
        "dataType": "json",
        "mergecontext": "",
        "output_location": "",
        "output_location_type": "none",
        "mustacheAltTags": false,
        "x": 450,
        "y": 220,
        "wires": [
            []
        ]
    },
    {
        "id": "b334bd9c.5672d",
        "type": "switch",
        "z": "ef1fe1e3.2f499",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "cont",
                "v": "on",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "off",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 390,
        "y": 420,
        "wires": [
            [
                "6608088e.2a22a8"
            ],
            [
                "cb2f4e1d.f3f4f"
            ]
        ]
    },
    {
        "id": "c84951da.617fd",
        "type": "server-state-changed",
        "z": "ef1fe1e3.2f499",
        "name": "",
        "server": "96ebf4f8.6d0e78",
        "version": 1,
        "exposeToHomeAssistant": false,
        "haConfig": [
            {
                "property": "name",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            }
        ],
        "entityidfilter": "light.sofa_lamp",
        "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": 140,
        "y": 420,
        "wires": [
            [
                "b334bd9c.5672d",
                "2c75a648.e2320a"
            ]
        ]
    },
    {
        "id": "6608088e.2a22a8",
        "type": "api-call-service",
        "z": "ef1fe1e3.2f499",
        "name": "Sofa on",
        "server": "96ebf4f8.6d0e78",
        "version": 1,
        "debugenabled": false,
        "service_domain": "light",
        "service": "turn_on",
        "entityId": "light.sofa_lamp",
        "data": "",
        "dataType": "json",
        "mergecontext": "",
        "output_location": "",
        "output_location_type": "none",
        "mustacheAltTags": false,
        "x": 580,
        "y": 360,
        "wires": [
            []
        ]
    },
    {
        "id": "2c75a648.e2320a",
        "type": "debug",
        "z": "ef1fe1e3.2f499",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "x": 410,
        "y": 540,
        "wires": []
    },
    {
        "id": "cb2f4e1d.f3f4f",
        "type": "api-call-service",
        "z": "ef1fe1e3.2f499",
        "name": "Sofa off",
        "server": "96ebf4f8.6d0e78",
        "version": 1,
        "debugenabled": false,
        "service_domain": "light",
        "service": "turn_off",
        "entityId": "light.sofa_lamp",
        "data": "",
        "dataType": "json",
        "mergecontext": "",
        "output_location": "",
        "output_location_type": "none",
        "mustacheAltTags": false,
        "x": 580,
        "y": 420,
        "wires": [
            []
        ]
    },
    {
        "id": "c4803627.12f8e8",
        "type": "ui_group",
        "z": "",
        "name": "Lounge",
        "tab": "f8f5c1b4.1a334",
        "order": 1,
        "disp": false,
        "width": "6",
        "collapse": false
    },
    {
        "id": "96ebf4f8.6d0e78",
        "type": "server",
        "z": "",
        "name": "Home Assistant",
        "addon": true
    },
    {
        "id": "f8f5c1b4.1a334",
        "type": "ui_tab",
        "z": "",
        "name": "Home",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]```