Launch an app on a Windows PC

I wouldn’t know where to begin, I have only really been looking into coding for a couple of weeks.

I can do basic things like write autohotkey, batch, and home assistant scripts but at the moment that’s where my knowledge stops.

1 Like

Thank you, but that looks like way to much for me at the moment, I recognize bits and pieces, but i have no clue what most it means or how to use it.

i think i need a few more months playing about before I try to create anything meaningful on my own.

It’s as simple as creating a .py file, and inputting the correct python code.

It can’t possibly be any harder than compiling and running java stuff. Screw JAVA.

1 Like

I tested it. It’s nice but I will try to figure out how to create to start it minimized.

I have written a script using autohotkey that will start this hidden and end it when you exit the AHK script, just start the run.exe.

source code is in the zip

there is a settings.ini so you can put your own settings in. (ignore the location setting)

https://drive.google.com/file/d/1eaZVphcuLu3z6gl8KZyDbnPHs2goRy0D/view?usp=sharing

1 Like

Thank you!

No problem,

I can’t do the hardcore coding yet like some of the guys here can, but I can do bits and pieces to make things easier.

Edit: HA doesn’t seem pass the JSON correctly for “winthing/system/commands/run”, I get mine to pass through node-red to clean up formatting before sending it to the PC so

HA-> Node-Red -> HA (broker) -> PC

This allows any .exe to run via direct JSON path name ie. [“C:\\Program Files (x86)\\Kodi\\kodi.exe”]

If you want me to share my Node-Red flow just me know.

Edit 2:
I used Autohotkey scripts to closes things, and for more control on the PC side, all AHK scripts can be exported as .exe which is nice.

2 Likes

I have noticed that winthing can be unresponsive after some time, so i have updated my script with new options in the settings.ini

Runmode and Runmode time

Runmode is used for restarting the service in case it fails

Runmodetime - Time in SECONDS before the service is restarted, service takes about 2 seconds to restart, suggested to keep this at about an hour

Runmode 1 - Will close the old service then start a new one

Runmode 2 - Will Start the new service then close the old one (crossover) this shortens the restart time but may result in double execution in rare cases

Runmode 3 - Off, this will not restart the service after any time

everything is included in the zip, all the details for winthing by msiedlarek are in the second post, all i have done is compile it and used an autohotkey script to make it hidden, easier to put in the settings and now keep it alive if it fails.

https://drive.google.com/file/d/1gF4tjT6nlcatGsUMQKdcocXxKLrmpxCD/view?usp=sharing

1 Like

How you trigger your flow from HA. Do you use MQTT switch or something else? I am planning to make it like an ACTIVATE button. But currently, I am using it as a switch.

I have an HA script for sending an MQTT command that node red listens for, then node red takes over and sends the actual MQTT command to the device.

I will explain better when I get home from work but it’s basically:
HA MQTT script, topic is a topic just for node red to listen for, payload can be anything you like.

So with the HA script I send, topic-node red, payload “do something 1”.

In node red have a switch attached to the MQTT listening module and if payload matches “do something 1”, send MQTT command, with payload “correctly formatted JSON for command” to topic “winthing topic you set”

Hope that gets you thinking for now, and I’ll post my flows later

I have NodeRED flows working and using them with HA as switch. When I get some time i will create scripts so I can use it with one press on ACTIVATE.

I’m Still new so im not sure I follow, sorry, do you mean like this?

Yes, like this. Currently I am using it as second one on the photo.

ah yes so here’s what I did in scripts.yaml have something like this:

mpcstartsteam:
    alias: "Media Start Steam"
    sequence:
    - service: mqtt.publish
      data:
        topic: mediapc
        payload: "startsteam"

or if you want use the editor, like this:

Then in Node-red:

Topic

Set Topic

Set Payload in HA with script

Payload can be anything, it’s coverted by the switch

Imgur


Switch

If payload contains “X” goto swtich "#
"


Msg Payload

Set Payload to link to .exe or other command

Double escaped


Send To Host

Sends message to host PC

Ensure “winthing” Server is running!

Topic is winthing/system/commands/open


Full View

Node-Red Flow:

[{“id”:“c5a928a.ecadad8”,“type”:“tab”,“label”:“Winthing Conversion Bridge”,“disabled”:false,“info”:“”},{“id”:“564f71ff.19fdf”,“type”:“mqtt out”,“z”:“c5a928a.ecadad8”,“name”:“Media PC Run Apps”,“topic”:“winthingmediapc/system/commands/open”,“qos”:“”,“retain”:“”,“broker”:“54e19abc.c77134”,“x”:788.0613784790039,“y”:207.54880905151367,“wires”:,“inputLabels”:[“["C:\\Steam.exe"]”]},{“id”:“de6c0612.177988”,“type”:“mqtt in”,“z”:“c5a928a.ecadad8”,“name”:“Media PC”,“topic”:“mediapc”,“qos”:“2”,“broker”:“54e19abc.c77134”,“x”:65,“y”:252.71788024902344,“wires”:[[“6d58845f.3cce2c”,“c2036a96.0f28d8”]],“outputLabels”:[“msg.payload”]},{“id”:“6d58845f.3cce2c”,“type”:“switch”,“z”:“c5a928a.ecadad8”,“name”:“”,“property”:“payload”,“propertyType”:“msg”,“rules”:[{“t”:“eq”,“v”:“startkodi”,“vt”:“str”},{“t”:“eq”,“v”:“stopkodi”,“vt”:“str”},{“t”:“eq”,“v”:“restart”,“vt”:“str”},{“t”:“eq”,“v”:“startteamviewer”,“vt”:“str”},{“t”:“eq”,“v”:“killteamviewer”,“vt”:“str”},{“t”:“eq”,“v”:“killsteam”,“vt”:“str”},{“t”:“eq”,“v”:“startsteam”,“vt”:“str”},{“t”:“eq”,“v”:“killorigin”,“vt”:“str”},{“t”:“eq”,“v”:“pcmr”,“vt”:“str”},{“t”:“eq”,“v”:“sleep”,“vt”:“str”}],“checkall”:“true”,“repair”:false,“outputs”:10,“x”:218.06148529052734,“y”:252.54875946044922,“wires”:[[“85d6d93.fad7b28”],[“d7480417.3a9e48”],[“f0485437.6186f8”],[“f4788483.1c8f88”],[“dcf7ad0e.9dc8d”],[“bcf6e24.8a4562”],[“dd542796.b6aca8”],[“3114cc74.983b24”],[“5d450d7a.b62474”],[“2394e232.fcd8ae”]]},{“id”:“85d6d93.fad7b28”,“type”:“change”,“z”:“c5a928a.ecadad8”,“name”:“Start Kodi”,“rules”:[{“t”:“set”,“p”:“payload”,“pt”:“msg”,“to”:“["C:\\Program Files (x86)\\Kodi\\kodi.exe"]”,“tot”:“str”}],“action”:“”,“property”:“”,“from”:“”,“to”:“”,“reg”:false,“x”:404.52369689941406,“y”:75.47617769241333,“wires”:[[“564f71ff.19fdf”,“c2036a96.0f28d8”]]},{“id”:“cedbc81a.63e8d8”,“type”:“comment”,“z”:“c5a928a.ecadad8”,“name”:“Topic”,“info”:“Set Topic\n\nSet Payload in HA with script\n\nPayload can be anything, it’s coverted by the switch”,“x”:83.85954666137695,“y”:24.377704620361328,“wires”:},{“id”:“a66d011a.18152”,“type”:“comment”,“z”:“c5a928a.ecadad8”,“name”:“Switch”,“info”:“If payload contains "X" goto swtich "X"”,“x”:218.4619026184082,“y”:25.960501670837402,“wires”:},{“id”:“12e57b6c.be4d25”,“type”:“comment”,“z”:“c5a928a.ecadad8”,“name”:“Msg Payload”,“info”:“Set Payload to link to .exe or other command\n\nDouble escaped ie "C:\\test\\test.exe"”,“x”:404.1761131286621,“y”:24.5319242477417,“wires”:},{“id”:“d9d2b732.293338”,“type”:“comment”,“z”:“c5a928a.ecadad8”,“name”:“Send To Host”,“info”:“Sends message to host PC \n\nEnsure "winthing" Server is running!\n\nTopic is winthing/system/commands/open”,“x”:746.5570831298828,“y”:24.293835639953613,“wires”:},{“id”:“d7480417.3a9e48”,“type”:“change”,“z”:“c5a928a.ecadad8”,“name”:“Close Kodi”,“rules”:[{“t”:“set”,“p”:“payload”,“pt”:“msg”,“to”:“["C:\\Users\\Media\\Desktop\\Start-up\\Control\\closekodi.exe"]”,“tot”:“str”}],“action”:“”,“property”:“”,“from”:“”,“to”:“”,“reg”:false,“x”:410.0236587524414,“y”:120.00947761535645,“wires”:[[“564f71ff.19fdf”]]},{“id”:“3abbcb12.6713b4”,“type”:“mqtt out”,“z”:“c5a928a.ecadad8”,“name”:“Restart”,“topic”:“winthingmediapc/system/commands/reboot”,“qos”:“”,“retain”:“”,“broker”:“54e19abc.c77134”,“x”:751.6903076171875,“y”:101.67611885070801,“wires”:,“inputLabels”:[“["C:\\Steam.exe"]”]},{“id”:“f0485437.6186f8”,“type”:“change”,“z”:“c5a928a.ecadad8”,“name”:“Restart”,“rules”:[{“t”:“set”,“p”:“payload”,“pt”:“msg”,“to”:“”,“tot”:“str”}],“action”:“”,“property”:“”,“from”:“”,“to”:“”,“reg”:false,“x”:408.35691833496094,“y”:166.67610359191895,“wires”:[[“3abbcb12.6713b4”]]},{“id”:“f4788483.1c8f88”,“type”:“change”,“z”:“c5a928a.ecadad8”,“name”:“Start TeamViewer”,“rules”:[{“t”:“set”,“p”:“payload”,“pt”:“msg”,“to”:“["C:\\Users\\Media\\Desktop\\Start-up\\Control\\startteam.exe"]”,“tot”:“str”}],“action”:“”,“property”:“”,“from”:“”,“to”:“”,“reg”:false,“x”:435.0236396789551,“y”:210.0094699859619,“wires”:[[“564f71ff.19fdf”]]},{“id”:“dcf7ad0e.9dc8d”,“type”:“change”,“z”:“c5a928a.ecadad8”,“name”:“Stop TeamViewer”,“rules”:[{“t”:“set”,“p”:“payload”,“pt”:“msg”,“to”:“["C:\\Users\\Media\\Desktop\\Start-up\\Control\\stopteam.exe"]”,“tot”:“str”}],“action”:“”,“property”:“”,“from”:“”,“to”:“”,“reg”:false,“x”:433.35699462890625,“y”:255.00946044921875,“wires”:[[“564f71ff.19fdf”]]},{“id”:“bcf6e24.8a4562”,“type”:“change”,“z”:“c5a928a.ecadad8”,“name”:“Stop Steam”,“rules”:[{“t”:“set”,“p”:“payload”,“pt”:“msg”,“to”:“["C:\\Users\\Media\\Desktop\\Start-up\\Control\\stopsteam.exe"]”,“tot”:“str”}],“action”:“”,“property”:“”,“from”:“”,“to”:“”,“reg”:false,“x”:418.35699462890625,“y”:298.3428039550781,“wires”:[[“564f71ff.19fdf”]]},{“id”:“dd542796.b6aca8”,“type”:“change”,“z”:“c5a928a.ecadad8”,“name”:“Start Steam”,“rules”:[{“t”:“set”,“p”:“payload”,“pt”:“msg”,“to”:“["C:\\Users\\Media\\Desktop\\Start-up\\Control\\startsteam.exe"]”,“tot”:“str”}],“action”:“”,“property”:“”,“from”:“”,“to”:“”,“reg”:false,“x”:420.0236511230469,“y”:340.00944995880127,“wires”:[[“564f71ff.19fdf”]]},{“id”:“3114cc74.983b24”,“type”:“change”,“z”:“c5a928a.ecadad8”,“name”:“Stop Origin”,“rules”:[{“t”:“set”,“p”:“payload”,“pt”:“msg”,“to”:“["C:\\Users\\Media\\Desktop\\Start-up\\Control\\stoporigin.exe"]”,“tot”:“str”}],“action”:“”,“property”:“”,“from”:“”,“to”:“”,“reg”:false,“x”:420.0236511230469,“y”:381.67611598968506,“wires”:[[“564f71ff.19fdf”]]},{“id”:“5d450d7a.b62474”,“type”:“change”,“z”:“c5a928a.ecadad8”,“name”:“Start Game Mode PCMR”,“rules”:[{“t”:“set”,“p”:“payload”,“pt”:“msg”,“to”:“["C:\\Users\\Media\\Desktop\\Start-up\\Control\\gamemode.exe"]”,“tot”:“str”}],“action”:“”,“property”:“”,“from”:“”,“to”:“”,“reg”:false,“x”:458.35698318481445,“y”:416.67613530158997,“wires”:[[“564f71ff.19fdf”]]},{“id”:“8ccbc82f.3875c8”,“type”:“mqtt out”,“z”:“c5a928a.ecadad8”,“name”:“Hibernate”,“topic”:“winthingmediapc/system/commands/hibernate”,“qos”:“”,“retain”:“”,“broker”:“54e19abc.c77134”,“x”:760.0236015319824,“y”:151.6761302947998,“wires”:,“inputLabels”:[“["C:\\Steam.exe"]”]},{“id”:“2394e232.fcd8ae”,“type”:“change”,“z”:“c5a928a.ecadad8”,“name”:“sleep”,“rules”:[{“t”:“set”,“p”:“payload”,“pt”:“msg”,“to”:“”,“tot”:“str”}],“action”:“”,“property”:“”,“from”:“”,“to”:“”,“reg”:false,“x”:401.69033432006836,“y”:455.0094575881958,“wires”:[[“8ccbc82f.3875c8”]]},{“id”:“c2036a96.0f28d8”,“type”:“debug”,“z”:“c5a928a.ecadad8”,“name”:“”,“active”:true,“tosidebar”:true,“console”:true,“tostatus”:true,“complete”:“payload”,“x”:240.5972023010254,“y”:505.8197202682495,“wires”:},{“id”:“54e19abc.c77134”,“type”:“mqtt-broker”,“z”:“”,“name”:“HA IO”,“broker”:“192.168.1.69”,“port”:“1883”,“clientid”:“”,“usetls”:false,“compatmode”:false,“keepalive”:“60”,“cleansession”:true,“birthTopic”:“”,“birthQos”:“0”,“birthPayload”:“”,“closeTopic”:“”,“closeQos”:“0”,“closePayload”:“”,“willTopic”:“”,“willQos”:“0”,“willPayload”:“”}]

1 Like

Thank you for everything!

For what it’s worth, you mention that HA doesn’t send the JSON correctly, try using:
"payload_template": "['lwin']"
This will remove the “{…}” wrapping the payload.

hey I am just trying to trigger a shortcut on a PC after I push a button (mqtt) is that possible?

yes, i put together one way with MQTT, Send an MQTT message from HA, and there’s a script that runs on your pc to carry out the actions. Launch an app on a Windows PC

I found it easier to use node red to carry out the actions,(Above) but it can all be done in HA

+1 for paho mqtt - minimal python script setup to run netflix in browser, launch plex media player below…

You’d run this on your HTPC and fill in the details to connect it to your MQTT broker e.g. mosquitto (installs in 5mins)

import paho.mqtt.client as mqtt
import subprocess
import ujson, webbrowser

MQ_CHANNEL = "SOME_CHANNEL_NAME"
USERNAME = "YOUR_MQTT_USERNAME"
PASSWORD = "YOUR_MQTT_PASSWORD"
MQTT_HOST = "127.0.0.1"
MQTT_PORT = 1883

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

    client.subscribe(f"{MQ_CHANNEL}/#")

def extract_payload(payload):
    try:
        return ujson.loads(payload.decode())
    except Exception:
        return payload.decode()

def command_plex(data):
    if data.get("args") == "start":
        subprocess.call(['C:\\Program Files\\Plex\\Plex Media Player\\PlexMediaPlayer.exe'])

def command_chrome(data):
    chrome_path = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
    if data.get("args") == "start_netflix":
        webbrowser.get(chrome_path).open("www.netflix.com")

def router(data):
    if data.get("command") == "plex":
        return command_plex(data)
    elif data.get("command") == "chrome":
        return command_chrome(data)

def on_message(client, userdata, msg):
    channel = msg.topic.replace(f"{MQ_CHANNEL}/", "")
    data = extract_payload(msg.payload)
    print(f"Received message on channel: {channel}, message: {data}")
    router(data)

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.username_pw_set(USERNAME, PASSWORD)

client.connect(MQTT_HOST, MQTT_PORT, 10)

client.loop_forever()

publish MQTT data from home assistant as json payloads:

  - service: mqtt.publish
    data:
      payload: '{ "command": "plex", "args": "start" }'
      topic: SOME_CHANNEL_NAME/myhtpcname
  - service: mqtt.publish
    data:
      payload: '{ "command": "chrome", "args": "start_netflix" }'
      topic: SOME_CHANNEL_NAME/myhtpcname