Send message outside an ui template

Hi all,
I have a ui template for a custom widget, within a html table.
This ui template needs to send out a message
A message arriving in the ui template triggers an output message.
To understand the code, I have removed all unnecessary code in the ui template to understand better.
The code is now quite simple, but can't make it work.
I can see in the console the "here!" message , but there is no message going out the ui template to the debug node

<script>
    (function(scope) {
        scope.$watch('msg', function(msg) {
        if (msg) {
            console.log("here!");
            scope.send({payload: "test_send"});
        }
        });
    })(scope);

</script>

[
    {
        "id": "a3ad21bb.8e08a8",
        "type": "ui_template",
        "z": "7474abc19b8e3f3e",
        "group": "ada4e0c8.ca2d58",
        "name": "",
        "order": 0,
        "width": 0,
        "height": 0,
        "format": "<h3>Testing template</h3>\n<p>{{msg.payload}}</p>\n<script>\n    (function(scope) {\n        scope.$watch('msg', function(msg) {\n        if (msg) {\n            console.log(\"Fire!\");\n            scope.send({msg.payload: \"youpi\"});\n            //$(\"#my_\"+scope.$id).html(msg.payload);\n        }\n        });\n    })(scope);\n</script>",
        "storeOutMessages": true,
        "fwdInMessages": false,
        "resendOnRefresh": true,
        "templateScope": "local",
        "className": "",
        "x": 420,
        "y": 160,
        "wires": [
            [
                "8105303d105fe438"
            ]
        ]
    },
    {
        "id": "9d387518.22ba48",
        "type": "inject",
        "z": "7474abc19b8e3f3e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": "",
        "topic": "",
        "payload": "One",
        "payloadType": "str",
        "x": 203,
        "y": 160,
        "wires": [
            [
                "a3ad21bb.8e08a8",
                "0a8b2dfa54fb14c7"
            ]
        ]
    },
    {
        "id": "8105303d105fe438",
        "type": "debug",
        "z": "7474abc19b8e3f3e",
        "name": "debug 73",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 620,
        "y": 160,
        "wires": []
    },
    {
        "id": "ada4e0c8.ca2d58",
        "type": "ui_group",
        "z": "7474abc19b8e3f3e",
        "name": "Default",
        "tab": "e4c8df5a.bc9048",
        "order": 1,
        "disp": true,
        "width": "6"
    },
    {
        "id": "e4c8df5a.bc9048",
        "type": "ui_tab",
        "z": "7474abc19b8e3f3e",
        "name": "tab2",
        "icon": "dashboard",
        "order": 1
    }
]

Well the template script you show is different to the script in your template flow.

Which are you trying to get to work?
If it's your template flow script then the msg.payload would require [ ] around msg.paylod, and uncheck add output to saved state.

It works for me

[{"id":"9d387518.22ba48","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"One","payloadType":"str","x":423,"y":7700,"wires":[["a3ad21bb.8e08a8"]]},{"id":"a3ad21bb.8e08a8","type":"ui_template","z":"d1395164b4eec73e","group":"ada4e0c8.ca2d58","name":"","order":0,"width":0,"height":0,"format":"<h3>Testing template</h3>\n<p>{{msg.payload}}</p>\n<script>\n    (function(scope) {\n        scope.$watch('msg', function(msg) {\n        if (msg) {\n            //console.log(\"Fire!\");\n            scope.send({[msg.payload]: \"youpi\"});\n            //$(\"#my_\"+scope.$id).html(msg.payload);\n        }\n        });\n    })(scope);\n</script>","storeOutMessages":false,"fwdInMessages":false,"resendOnRefresh":true,"templateScope":"local","className":"","x":640,"y":7700,"wires":[["8105303d105fe438"]]},{"id":"8105303d105fe438","type":"debug","z":"d1395164b4eec73e","name":"debug 73","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":840,"y":7700,"wires":[]},{"id":"ada4e0c8.ca2d58","type":"ui_group","z":"d1395164b4eec73e","name":"Default","tab":"e4c8df5a.bc9048","order":1,"disp":true,"width":"6"},{"id":"e4c8df5a.bc9048","type":"ui_tab","z":"d1395164b4eec73e","name":"tab2","icon":"dashboard","order":1}]

Thanks E1cid, I found why the template script was not working in my project: The template was in the wrong dashboard, so was not in the scope...
I put it in the right dashboard and it works
I don't need to add brackets around msg.payload, it works without.