Windows Desktop -> MQTT -> HA

Hello,

I would be very happy if someone could point me in the right way for a little project I have in mind.

I do have a few Flux Led RGB strips I can already control with HA. My goal is to be able to make them react to conditions from my PC (Win 10).

So the steps would be:
1- my PC publishes an MQTT message to Mosquito (for example turn on strip 1, red color)
2- HA gets the MQTT message and process it (script, automation, etc.).

I don’t know how to approach step 2: how do I make HA to process a MQTT message? I tried to look in the doc and across the forum but I can’t find a good example to help me. Should I create a MQTT sensor or MQTT binary sensor? How can HA handle it in an automation or script?

Any help will be greatly appreciated as this is my real first attempt to do a custom project.

JP

The most direct method is to use the MQTT message as a trigger in an automation.

You could also do as you say, and create MQTT sensors and trigger off the state of the sensors.

ok, Thank you for the lead. I will do some tests.

After some tests, I have a working prototype. I’m posting this setup if someone can benefit:
1- Create a MQTT sensor

- platform: mqtt
  state_topic: "rgb/strip1/status"  
  name: "RGB Strip1"  

2- install Mosquito (https://mosquitto.org/download/) and AutoHotKeys (https://www.autohotkey.com/) on your Windows computer

3- setup a autohotkey script (note I’m using Rapidhotkey lib)

~ScrollLock::RapidHotkey("Toggle",2,,1)
Toggle: 
run "c:\Program Files (x86)\mosquitto\mosquitto_pub.exe"  -h 192.168.1.12 -t rgb/strip1/status -m On -P password -u user,,hide
return

4- create an automation in HA

- id: '1521911309997'
  alias: Nouvelle automatisation
  trigger:
  - platform: mqtt
    topic: rgb/strip1/status
 action:
    service_template: >
       {% if states.light['6001949557bd_1921681149'].state == 'on' %}
       light.turn_off
       {% else %}
       light.turn_on 
       {% endif %}
    data:
        entity_id: light.6001949557bd_1921681149

In the end, if I double-tap “scrollLock” key on my keyboard, it will toggle “On” or “Off” the rgb strip. I will expand this setup to send visual alerts or to “hotkey” control devices from my main computer…

5 Likes

Just to document a little bit more as I struggled a bit
on step 2 and 3
Mosquito:

  • Install https://mosquitto.org/download/ (and note down where the file “mosquitto_pub.exe” is on your computer
  • Install also the mosquitto add-on to Home Assistant (I’m on Hassio so it’s a bit out of the box)
  • on the add-on, make sure you check the documentation. I had to create a new user in HA in order to send a username and password by the script (next step)

Autohotkey - Rapidhotkey

  • You need to add the RapidHotkey library.
  • To do this, download the library here
  • click on “raw”, copy the code, paste in notepad
  • save as “RapidHotkey.ahk” use the quotes in your filename
  • Find on your computer where autohotkey.exe is. Add a new folder called /Lib if it’s not there already. Move the notepad file in that folder.

Autohotkey - Script

  • replace in the script the link location to mosquitto_pu.exe as mentioned above.
  • Change “password” into the password you made in mosquitto addon process above.
  • Same for user.
  • The script is made to trigger a toggle animation in Home Assistant when you click twice on scroll lock

Automation

  • Adjust the automation script to fit your action.

Thanks for the update. I have gone through this process and made a few changes just wanted to add some more detail that may help others.

In the case of RapidHotkey library section, unless you need to activate a hotkey by repeatedly clicking a certain key, this part really isn’t necessary and removing it will make the process less complicated. My Goal was the hold Home key and press a # or Letter.

For example, in my script file, I used:

Home & 1::Run "c:\Program Files\mosquitto\mosquitto_pub.exe"  -h 10.0.1.19 -t keyboard/loudesk/status -m 1 -P password -u user,,hide

It’s also possible to combine them for a full list of hotkeys using just one script file.
Example:

Home & 1::Run "c:\Program Files\mosquitto\mosquitto_pub.exe"  -h 10.0.1.19 -t keyboard/loudesk/status -m 1 -P password -u user,,hide
Home & 2::Run "c:\Program Files\mosquitto\mosquitto_pub.exe"  -h 10.0.1.19 -t keyboard/loudesk/status -m 2 -P password -u user,,hide
Home & 3::Run "c:\Program Files\mosquitto\mosquitto_pub.exe"  -h 10.0.1.19 -t keyboard/loudesk/status -m 3 -P password -u user,,hide

In my automations, I also plan to set the status of the MQTT to Waiting upon completion, this way my automation triggers can be ‘if X changes from Waiting to 1’ etc, Otherwise if I use Home+1 to set a lighting scene at 8am, and then the next command I use is also Home+1, I don’ think it will trigger a “to” since it was already the same value. (This might not be needed, but I like the idea of in the least, if I’m trying to debug something, I can tell the difference between an old command, and watching one happen.)

Lastly, I copied the script file into a Dropbox folder, and then made a shortcut to it, and put it in the Start Menu \ Startup folder, so it auto runs. Just thought this might help someone else. The dropbox idea allows me to keep organized track of this function as it grows, but also so that if I choose to set up the same function on other PC’s in the house, I can make changes to the script on one of them, and have ithe others update upon next windows reboot.

Credit to both original OP and ikeays, This is incredible.

1 Like

This is awesome, thanks! One thing I added was a tilde in front of the word Home so that AutoHotKey doesn’t block the Home key.

So without the tilde, the home key is “disabled”, but with the tilde, the home key works as normal and the automations work too.

Example:

~Home & 1:: Run......

Very helpul thanks. I ended up using other alternative skipping the MQTT layer.
In fact you call direclty from Autohotkey any service/ automation with the HA API .

Example:

;Control-Alt-L will toggle my light
^!l:: Run curl -X POST -H "Authorization: Bearer %Authorization%" -H "Content-Type: application/json" -d {"""entity_id""":"""light.mylight_light"""} http://192.168.1.xx:8123/api/services/light/toggle,,hide

Replace : %Authorization% by your token and adapt the service call
Curl is included natively in recent Windows 10 builds so no need for extra installation

1 Like

Yes the HA API is faster I think. But maybe mqtt is userfull to have the possibility to send command to the pc… My idea was to use a light sensor (already configurated and working in home assistant) to edit the Screen Bright of my desktop monitor using the program ScreenBright in command line… Any of you that are using MQTT + autohotkey + windows pc is able to send command from home assistant ->mqtt -> autohotkey (windows pc) so I can change the brightness of my monitor?

I don’t use autohotkey for that. Have a look to IOT Link software. It’s MQTT based and running as a service on Windows. It provide integration with HA.

[https://gitlab.com/iotlink/iotlink/-/wikis/Addons/Commands](IOT Link)

You can execute a command on your PC from HA. You have built in like volume but also any custom program
I use it to change the brighntess of my DELL desk monitor based on an illumance sensor in the room

1 Like

You are the best! I downloaded it yesterday and it works great! thanks for the asnwer!

I’m using “HASS Workstation Service” for some time no, which is working very stable:

I was using IOT Link before and basicly that was doing its job as well. But I’ve had some problems with the service sometimes when the computer started from standby.

1 Like

Thank you. I will try HASS Workstation Service
I have some statbility issue too with IOT Link when resuming from sleep.

This sound pretty cool, would you mind sharing your configuration files for the brightness adjustments?

I would be interested in the automation you are using on the homeassistant side and iotlink configuration on the windows side.

Hi,

In short I have an automation that call this script every time the illuminance of y room change.
It set up a slider as a variable for the brightness level

alias: PC - Luminosité Ecran - set number
sequence:
  - data_template:
      value: >
        {% set lux = states('sensor.illuminance') | float %}
        {% if lux <= 1 %}
          6
        {% elif lux <= 2 %}
          10
        {% elif lux <= 4 %}
          12
        {% elif lux <= 6 %}
          16
        {% elif lux <= 10 %}
          18
        {% elif lux <= 20 %}
          20
        {% elif lux <= 40 %}
          30
        {% elif lux <= 80 %}
          40
        {% elif lux <= 120 %}
          50
        {% elif lux <= 150 %}
          55
        {% elif lux <= 250 %}
          65
        {% elif lux <= 300 %}
          70
        {% elif lux <= 400 %}
          85
        {% elif lux <= 550 %}
          95
        {% else %}
          99
        {% endif %}
    service: input_number.set_value
    entity_id: input_number.pc_luminosite_ecran
mode: single

Then I have a second automation that sets the brighnesst when my PC is considered online

service: mqtt.publish
data:
  topic: iotlink/workgroup/r3/commands/run
  payload: >-
    { "command": "C:\\Program Files (x86)\\Dell\\Dell Display Manager\\ddm.exe",
    "args": "/SetBrightnessLevel {{
    states('input_number.pc_luminosite_ecran')|int  }}", "path": "C:\\Program
    Files (x86)\\Dell\\Dell Display Manager", "user": "", "visible": true,
    "fallback": true }

But overall this setup is pretty unreliable because of IOT link. I have a lot of miss when I resume from sleep. The thing is i’m not able to reproduce similar setup in hass workstation service because at this stage it does not support custom command with variables.

net stop "iot link" followed by net start "iot link" in a batch file which Windows Task Scheduler runs (via the system account) on resume from sleep could be a hacky workaround.