IDK
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.
IDK
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?
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.
Again, have you tested publishing a message, from batch, on a windows restart? (I already know your broker is working)
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?
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
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.
Your assumption is correct. According to this, HA received the command… is the structure correct? No entry, error or otherwise, in HA logs.
HA won’t see the message until you create a Sensor to ‘receive’ it.
The one I created for this pub message looks like this: (NOTE: This will create generic mqtt sensor entity, so you’ll need to rename the entity in HA)
- platform: mqtt
unique_id: '123456HomeAssistantShutDown'
state_topic: "homeassistant/CMD/shutdown"
value_template: "{{ value_json.cmd }}"
So now that we confirmed my pub message works from command line, now we need to build up the message string for your example. Did you create the sensor in HA for IoT Link, or did it create one for you (auto discovery)?
I can’t get the sensor to work… I created the sensor, renamed the entity to sensor.cmd_shutdown. Renamed uique_id to cmd_shutdown (not sure if that was necessary). Rebooted HA
- platform: mqtt
unique_id: 'cmd_shutdown'
state_topic: "homeassistant/CMD/shutdown"
value_template: "{{ value_json.cmd }}"
I created the Session Status 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"
qos: 1
The id must be unique, I’m not sure if it needs to be different from the name (I doubt it).
That said, in HA (using the mqtt integration) listen for the message on homeassistant/CMD/#
I know the combination works, because I’m using it…
So using this HA sensor:
- platform: mqtt
unique_id: '123456HomeAssistantIOT'
name: "My Computer - Session Status"
state_topic: "iotlink/home/haf922/windows-monitor/status"
value_template: "{{ value_json.alias }}"
And this message:
{\"alias\":\""Computer Unlocked\"",\"data\":\"SessionUnlock\"}
I get this in HA
Thank you for all your input on this!