Run a task before turning plug off

Hi Team

Hoping someone might be able to help. So i have this flow.

When the XBOX plug turns on it then changes the HDMI port to HDMI2 and then turns the xbox one on.

What i am trying to work out is the power off process. What i have now is when the XBOX plug turns off it does exactly that. What i really want to do is power the XBOX off via the Media player in the same way it turns it on , then physically power off the XBOX plug.

Any ideas or suggestions?

[{"id":"79eb09ac0fa4362a","type":"group","z":"d05590133df5ebcc","name":"XBOX Modes","style":{"label":true},"nodes":["5bdeca4359c40f1c","f1d5ac43416c385c","c8569a07cb1cce56","330f65d720e83fcc"],"x":34,"y":819,"w":672,"h":162},{"id":"5bdeca4359c40f1c","type":"server-state-changed","z":"d05590133df5ebcc","g":"79eb09ac0fa4362a","name":"XBOX: ON","server":"7742f894.e9abc8","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"switch.plug_living_room_xbox","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","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,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":120,"y":860,"wires":[["f1d5ac43416c385c"],["330f65d720e83fcc"]]},{"id":"f1d5ac43416c385c","type":"api-call-service","z":"d05590133df5ebcc","g":"79eb09ac0fa4362a","name":"AvSTAR HDMI2","server":"7742f894.e9abc8","version":5,"debugenabled":false,"domain":"script","service":"avstar_hdmi2","areaId":[],"deviceId":[],"entityId":[],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":320,"y":860,"wires":[["c8569a07cb1cce56"]]},{"id":"c8569a07cb1cce56","type":"api-call-service","z":"d05590133df5ebcc","g":"79eb09ac0fa4362a","name":"XBOX Turn : ON","server":"7742f894.e9abc8","version":5,"debugenabled":false,"domain":"media_player","service":"turn_on","areaId":[],"deviceId":[],"entityId":["media_player.xbox"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":600,"y":860,"wires":[[]]},{"id":"330f65d720e83fcc","type":"api-call-service","z":"d05590133df5ebcc","g":"79eb09ac0fa4362a","name":"XBOX Turn : OFF","server":"7742f894.e9abc8","version":5,"debugenabled":false,"domain":"media_player","service":"turn_off","areaId":[],"deviceId":[],"entityId":["media_player.xbox"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":330,"y":940,"wires":[[]]},{"id":"7742f894.e9abc8","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]