Send MQTT payload from IoT Link to HA via Windows CMD

I’m using IoT Link to send HA status messages, ie. SessionUnlock, etc. The problem I’m running into is when I first boot into Windows, the VM hassio is installed on is not running yet, hence, HA is not active to receive the payload from IoT Link.

Best thing I can think of is to use Windows Task Manager to send an appropriate payload to HA in X mins. However, I cannot find any instruction on how to send it via BAT file, or similar. Suggestions on how to do that, or a completely different way of tackling this?

Is your MQTT broker part of Home Assistant or Windows?

HA, via add-on.

Then IOTLink does not receive any commands until HA is started.

Thanks for the reply, though I think it’s the opposite. IoT Link is installed on my Windows host PC.

When my host loads the Windows Session, IoT Link, via mqtt, sends out a payload (SessionUnlock) for which HA has a sensor listening for that payload. Upon host initial boot and login, my HA guest VM has not started yet… It’s not active to sense the payload sent out by IoT Link, so the payload never gets reacted to by HA.

Note, it all works fine when my host is already running and say I Lock and/or Unlock the Windows Session. Payloads are responded to accordingly.

IOTlink sends via mqtt, but your mqtt server is in a VM that is not started yet ? So nothing gets sent.

Ah, I understand now, thanks. So what must be occurring in my case is IoT Link starts the message, but there’s no mqtt broker to send it on. By the time HA is started, or actually restored from a saved state in my case, I’m guessing there’s no longer a message to send.

I’m using hyper-v; they already auto-start. In my case, HA doesn’t know it’s being restored, at least none of the possible commands/sensors I’ve tried in HA fire off upon restoring. HA Start sensor works when HA is started fresh, but not from a VM restore.

I say the above as there’s nothing HA can sense to fire my Session-related script I do have in HA.

I see 3 possible options…

  1. If I had a way to send a mqtt payload via BAT file, I could schedule it to run X mins upon boot and initial login.
  2. The other option may be to react to the VM being restored, but not certain how to use the hyper-v manager to determine this.
  3. Install mqtt broker on host, though not sure how to handle the cert-related steps.

Personally, I would decouple the broker from HA / VM and set it up as a service on windows. This will allow greater flexibility IMO.,

If you did, you could do something like this.

@ECHO ON
setlocal EnableDelayedExpansion

SET MOS="C:\Program Files\mosquitto\mosquitto_pub.exe"

%MOS% -h 192.168.1.26 -t homeassistant/CMD/shutdown -m {\"type\":\"HA\",\"cmd\":\"SHUTDOWN\"} -u USERNAME -P PASSWORD

In following the below guide to install/run mqtt on Windows, I do have a few questions…

  1. In your example, what is the username/pass of, HA?
  2. Regarding security… if the above is of HA, how is mqtt secured? I realize I can add user/pass to the mqtt config, but how is that passed on?
  3. If I want to send an IoT Link command, how would I structure the ‘Topic’ and ‘payload’ in order for HA to handle it via sensor?
  4. If I use the windows-based mqtt, do I need to uninstall the mqtt broker HA add-on, or I suppose simply stopping it would be sufficient?

Thanks!

Guide:
https://bytesofgigabytes.com/mqtt/installing-mqtt-broker-on-windows/

  1. Decoupling the MQTT broker requires shutting down the HA broker (or I guess you could use a different port) then installing only the MQTT integration in HA, at this point HA becomes only a client and not the broker. Then installing and setting up on Windows.

Edit: The username & password are for the broker.

  1. I have no experience setting up certs for encrypted MQTT coms (some of my devices can’t use it). I only have Auth. set to username and password which there are easy to follow instructions on how to do this on line (check link below).

  2. json, look at my example.

  3. I wouldn’t remove the HA broker until you have the new network up and running. Just shut it down and don’t allow it to restart.

Notes:
I’m running the 64 bit version V2 as a windows service.
Steve has some fantastic information on all things MQTT

Thanks for the input and help links. I’ve got it installed, running w / user/pass, and connects in MQTT Explorer.

In running the ‘re-configure mqtt’ option in my original mqtt integration, I receive a ‘Failed to connect’ error message. Assuming I’m to place my Windows host PC IP address here, I’m wondering if it’s because it is not secured. My HA runs behind a proxy and think the mqtt would need SSL in order to connect?

https://ibb.co/m0NSHwJ

I don’t know what that means.

It’s the HA integration for mqtt, but there’s an option to re-configure the existing. The broker field is not accepting my IP. I’m thinking I need to have mqtt secured in order to communicate thru the nginx proxy.

This screen ?

image

Did you open the Windows firewall for mqtt traffic ?

Nginx is for http/https traffic, has nothing to do with mqtt traffic (totally different protocol)

  1. So from another machine you can connect to the new windows broker with MQTT Explorer?

  2. If so, to be clear, the only client you are having problems connecting to the new broker is HA?

I rebooted my PC and HA, re-configured the mqtt integration and got it working, finally.

1 Like

Could you help me with this. I’m not understanding the syntax of the code. Ultimately, I’m trying to… Publish a topic & payload from IoT Link to HA. I’m not certain how to do this.

@ECHO ON
setlocal EnableDelayedExpansion

SET MOS="C:\Program Files\mosquitto\mosquitto_pub.exe"

%MOS% -h 10.9.8.1.22 -t homeassistant/ -u user -P pass

I can try to help, just be warned I’m new at this… :crazy_face:

Can you post the json or details of the message you want to send.

%MOS% -h 192.168.1.26 -t homeassistant/CMD/shutdown -m {\"type\":\"HA\",\"cmd\":\"SHUTDOWN\"} -u USERNAME -P PASSWORD

%MOS% is just a variable set to the path and program to mosquitto_pub.exe

This page will help you understand the structure for sending a message using mosquitto_pub.exe

We’ll learn together :grinning:

Where did you learn/find how to form a message for mqtt to send? I get how it looks like in HA, but don’t understand how to form the topic and payload in the mqtt message.

Below is json for a script and sensor of Session Status

Script:

{"alias":"Computer Unlocked","sequence":[{"service":"mqtt.publish","data":{"payload":"SessionUnlock","topic":"iotlink/home/haf922/windows-monitor/status"}}],"mode":"single"}

Sensor:

[{"platform":"mqtt","name":"My Computer - Session Status","state_topic":"iotlink/home/haf922/windows-monitor/status","unit_of_measurement":"","icon":"mdi:account","value_template":"{{ value }}","availability_topic":"iotlink/home/haf922/lwt","payload_available":"ON","payload_not_available":"OFF"}]

that’s the message being sent from iotlink?

Lot’s of help, trial and frustration. :crazy_face: