On/off buttons syncing with HA

I appear to have fallen at the first hurdle for creating my dashboard.

I can set up a switch that will turn the light on and off but it's not in sync if something like a sensor turns the light on or off.

I put and events: state in to detect a change in the state of the entity and inject that into my switch node which then turns the light on or off.

It seems to work most of the time but sometimes it gets messed up and turned the light on and off repeatedly - I've blown an LED controller this afternoon due to this. This is my flow, with the code below.

I'm sure this can't be the right way. I'd be interested to know what the best way of doing this is.

    {
        "id": "8ecc988f.dad0b8",
        "type": "server-state-changed",
        "z": "d18850c2.58c1",
        "name": "Landing Light Check",
        "server": "7b1d3390.03561c",
        "version": 1,
        "exposeToHomeAssistant": false,
        "haConfig": [
            {
                "property": "name",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            }
        ],
        "entityidfilter": "switch.landing_ceiling_shelly",
        "entityidfiltertype": "exact",
        "outputinitially": false,
        "state_type": "habool",
        "haltifstate": "true",
        "halt_if_type": "bool",
        "halt_if_compare": "is",
        "outputs": 2,
        "output_only_on_state_change": true,
        "for": 0,
        "forType": "num",
        "forUnits": "minutes",
        "ignorePrevStateNull": false,
        "ignorePrevStateUnknown": false,
        "ignorePrevStateUnavailable": false,
        "ignoreCurrentStateUnknown": false,
        "ignoreCurrentStateUnavailable": false,
        "x": 90,
        "y": 480,
        "wires": [
            [
                "a4ca29b9.xxxxxx"
            ],
            [
                "a4ca29b9.xxxxxx"
            ]
        ]
    },
    {
        "id": "a4ca29b9.xxxxxx",
        "type": "ui_switch",
        "z": "d18850c2.xxxxxx",
        "name": "",
        "label": "Light",
        "tooltip": "",
        "group": "cb5e63c3.xxxxxx",
        "order": 2,
        "width": 6,
        "height": 2,
        "passthru": true,
        "decouple": "false",
        "topic": "topic",
        "topicType": "msg",
        "style": "",
        "onvalue": "true",
        "onvalueType": "bool",
        "onicon": "fa-lightbulb-o fa-4x",
        "oncolor": "orange",
        "offvalue": "false",
        "offvalueType": "bool",
        "officon": "fa-lightbulb-o fa-4x",
        "offcolor": "gray",
        "animate": false,
        "x": 330,
        "y": 480,
        "wires": [
            [
                "a29daba6.xxxxxx"
            ]
        ]
    },
    {
        "id": "a29daba6.xxxxxx",
        "type": "switch",
        "z": "d18850c2.xxxxxx",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "true"
            },
            {
                "t": "false"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 490,
        "y": 480,
        "wires": [
            [
                "d50ed502.xxxxxx"
            ],
            [
                "ff76b90f.xxxxxx"
            ]
        ]
    },
    {
        "id": "d50ed502.xxxxxx",
        "type": "api-call-service",
        "z": "d18850c2.xxxxxx",
        "name": "Landing Light On",
        "server": "7b1d3390.xxxxxx",
        "version": 1,
        "debugenabled": false,
        "service_domain": "switch",
        "service": "turn_on",
        "entityId": "switch.landing_ceiling_shelly",
        "data": "",
        "dataType": "jsonata",
        "mergecontext": "",
        "output_location": "",
        "output_location_type": "none",
        "mustacheAltTags": false,
        "x": 670,
        "y": 440,
        "wires": [
            []
        ]
    },
    {
        "id": "ff76b90f.xxxxxx",
        "type": "api-call-service",
        "z": "d18850c2.xxxxxx",
        "name": "Landing Light Off",
        "server": "7b1d3390.xxxxxx",
        "version": 1,
        "debugenabled": false,
        "service_domain": "switch",
        "service": "turn_off",
        "entityId": "switch.landing_ceiling_shelly",
        "data": "",
        "dataType": "jsonata",
        "mergecontext": "",
        "output_location": "",
        "output_location_type": "none",
        "mustacheAltTags": false,
        "x": 670,
        "y": 520,
        "wires": [
            []
        ]
    },
    {
        "id": "7b1d3390.xxxxxx",
        "type": "server",
        "name": "Home Assistant",
        "addon": true
    },
    {
        "id": "cb5e63c3.xxxxxx",
        "type": "ui_group",
        "name": "Landing",
        "tab": "d9ec6cf6.xxxxxx",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "d9ec6cf6.xxxxxx",
        "type": "ui_tab",
        "name": "Misc",
        "icon": "fa-random",
        "order": 8,
        "disabled": false,
        "hidden": false
    }
]