Send MQTT payload from IoT Link to HA via Windows CMD

  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:

IDK :grinning:

Those are what’s in HA, ie. the sensor HA is using to listen for the message IoT Link sends out.

This is what I’m trying to figure out… How should the mqtt message be structured.

Use mqtt explorer to copy the message being sent.

I guess I also need to better understand your goal(s). Would adjusting the retain and QOS solve the problem?

  1. Windows Host boots
  2. Your IoT Link service starts
    2b) Along with your newly decoupled MQTT broker.
  3. When HA connects to the broker it should pull the last retained msg that IoT Link sent.

MQTT Explorer does not provide me the actual message structure. It shows me the Topic and Payload, but I already know those. See screenshot below.

The problem is, the HA VM is ‘saved’ when my PC reboots/shuts down. I’m using hyper-v… not aware of an option to change this to shutdown instead of saved.

What this means… when the Windows starts, the HA VM is restored, but HA does not know this… it’s not like HA is starting fresh.

I may have missed it, but what is your goal and purpose?

But first things first, before we even get to that. Have you tried sending a message using mosquitto_pub.exe , on Windows startup to make sure the broker sees the message?

You can just use my example and add -q 2 -r (qos and retain flag) at the end.

Thanks for the all the input, much appreciated. I know the broker is working. HA receives the appropriate SessionUnlock and other messages when HA is active.

The disconnect here is about the VM restoration process. When shutting down Windows, my HA hyper-v based VM is automatically placed in a saved state… HA is not shutdown. So, when I log in, the HA VM is restored. During this process, HA is not being started. the VM is being restored; there’s nothing for it to do because it’s been restored, not started.

This is why I mentioned the part about whether hyper-v can auto-shutdown instead of save the VM. So far, I haven’t found a way.

Simply put… if I could just figure out how to structure the proper “Topic” and “Payload”, I would be golden.

  1. Again, have you tested publishing a message, from batch, on a windows restart? (I already know your broker is working)

  2. I understand the problem as you have presented it, but what is unclear to me is WHY you need to send HA a message from batch on VM restoration, that a retained message can’t handle?

  3. and yes you can configure Hyper-V to shut the VM down on an Host shut down, instead of saving.

VM properties >> settings >> Automatic Stop Action

Oh and you’ll want to use shut down guest OS option

Updated comments…

I will try the following BAT on reboot…

@ECHO ON
setlocal EnableDelayedExpansion

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

%MOS% -h 10.9.8.22 -t homeassistant/CMD/shutdown -m {\"type\":\"HA\",\"cmd\":\"SHUTDOWN\"} -u user -P pass -q 2 -r (qos and retain flag)

The above message does not work, either manually executing it or as a startup task. (I had the proper user/pass in the bat)

I’ll look into this.

Shutting down HA on PC shutdown or reboot and having it auto start upon Windows logon did not update w/ any retained messages.

I know HA can see/receive messages via my now PC-based mqtt broker because if I simply Lock the PC and log back in, the Session Status within HA properly changes. This would not happen w/o IoT Link sending out the message via mqtt.

With regards to ‘retained messages’, here’s some observations…

With the hyper-v option to auto shutdown the VM upon Windows shutdown/reboot…

Upon reboot, if I logon right away, the Session Status is ‘Unknown’. If I wait a few minutes, to let HA fully start, upon logon, the message is then SessionLogon. Regardless, HA is not seeing the SessionUnlock message.

After logging into Windows and everything is running, if I Lock my session, HA will report a proper Session Status of SessionLock or ConsoleConnect (confirmed by reviewing such in my Android HA app)… both seem to be acceptable acknowledgements that the Windows session is locked. Upon logging back into Windows, the HA Session Status properly changes to SessionUnlock.

The simple solution to this is what I’ve been after, I need a working BAT to send the message SessionUnlock to HA from IoT Link.

This is incorrect (you’ve added (qos and retain flag))

Path of mosquitto_pub.exe  -h 10.9.8.22 -t homeassistant/CMD/shutdown -m {\"type\":\"HA\",\"cmd\":\"SHUTDOWN\"} -u user -P pass -q 2 -r (qos and retain flag)

Try this (first try in an elevated command prompt, if that works then on boot from start (Remember to remove and add you path of Path of mosquitto_pub.exe)

Path of mosquitto_pub.exe -h 10.9.8.22 -t homeassistant/CMD/shutdown -m {\"type\":\"HA\",\"cmd\":\"SHUTDOWN\"} -u user -P pass -q 2 -r

I receive the error… “No connection could be made because the target machine actively refused it”

I tried adding the port “-p 8123” and that resulted in the following error… “Error: A network protocol error occurred when communicating with the broker”

I’m assuming the 2nd error is in regards to the mqtt broker, I don’t know about the 1st error.

I did previously overlook the help link you provided… thanks for that. I understand -t being the topic, but I cannot figure out how to present the payload. The only associated attribute I see is will-payload.

Did you change the broker’s default (1883) port to 8123? which is the port to HA?

The host address needs to be the IP address for your mqtt broker not HA.

When you set the broker up if you use the standard 1883 port you don’t need to include it in the pub string.

Pardon it’s like teaching a kid to ride a bike :upside_down_face:

I changed the host ip to local IP and tried ‘localhost’. Although no error messages now and it executed, no result.

I enabled mqtt logging; below is the output… I don’t see any issues.

2022-05-08T19:51:42: mosquitto version 2.0.14 starting
2022-05-08T19:51:42: Config loaded from C:\Program Files\mosquitto/mosquitto.conf.
2022-05-08T19:51:42: Opening ipv6 listen socket on port 1883.
2022-05-08T19:51:42: Opening ipv4 listen socket on port 1883.
2022-05-08T19:51:42: mosquitto version 2.0.14 running
2022-05-08T19:51:46: New connection from 10.9.8.13:57864 on port 1883.
2022-05-08T19:51:46: New client connected from 10.9.8.13:57864 as HAF922 (p2, c1, k60, u'user').
2022-05-08T19:51:53: New connection from ::1:57904 on port 1883.
2022-05-08T19:51:53: New client connected from ::1:57904 as auto-C7D91257-35C3-0F99-84AB-DF91713C921D (p2, c1, k60, u'user').
2022-05-08T19:51:53: Client auto-C7D91257-35C3-0F99-84AB-DF91713C921D disconnected.
2022-05-08T19:52:37: mosquitto version 2.0.14 terminating

Did you have MQTT Explorer running when you ran the pub command? I’m thinking If you didn’t get any errors when you ran the command, then most likely it was sent, and if so it would show up in MQTT Explorer.