Using IoT Link & mqtt to monitor my host PC for session locking, logging off

I have successfully connected IoT Link from my host PC to my HA VM via mqtt. I can run scripts in HA to do things on my host like Lock Windows session, etc. However, I’m at a loss how to write an Automation to monitor my PC for when the computer locks, logoff, etc. Ideas?

BTW, I followed the below guide to get it all connected:
https://flemmingss.com/using-iot-link-to-control-and-monitor-windows-10-11-computers-from-home-assistant/

Those are ‘commands’. I’m wanting to know how to ‘monitor’ for these type of actions that occur on my host PC by other means.

Say I manually log out of my host PC, and I want my desk light to be turned off. I can certainly, manually turn off my light, but I want IoT Link and HA to monitor my host PC for the logoff action and react to it. Can this be accomplished?

Ah so. On the bottom of that page they talk about observation. Can you monitor mqtt and see what happens on the topic.

A good tip to see mqtt realtime:

Thank you. I actually have that running and see a lot of info, but not sure what I should be looking for that I can get HA to react to :upside_down_face:

The ‘Observation’ on that page is only referring to prefixes, etc on that page, nothing to do with monitoring.

Approach like the karate kid: wax on - wax off. Log on, log off ans see what happens?

Ok, I believe I found the IoT Link-related stat I want to monitor and react to. However, I’m having trouble adding it as a HA ‘sensor’.

Below is the code I’m working with from a similar sensor. Based on the info in the MQTT Explorer screenshot, how do I fit it into the sensor code?

What I will do is create an Automation that reacts to whether the sensor indicates SessionLock or SessionUnlock.

  - 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

MQTT Explorer screenshot:

Your topic is not equal? Screenshot vs code?

Edit: never mind, it is. Sorry.

Try starting with a very simple sensor as top example in
MQTT Sensor - Home Assistant :man_shrugging:

I’m not sure the code is correct… That’s my inquiry. This code is from another sensor, but how do I figure out what the payload is, other than trial & error? For the next time… Where do I get the payload from?

I think it’s not correct, but I am not sure.

See here: MQTT Sensor - Home Assistant

availability list (optional)

A list of MQTT topics subscribed to receive availability (online/offline) updates. Must not be used together with availability_topic.

Should like this

# Example configuration.yaml entry
sensor:
  - platform: mqtt
    name: "RSSI"
    state_topic: "home/sensor1/infojson"
    unit_of_measurement: "dBm"
    value_template: "{{ value_json.RSSI }}"
    availability:
      - topic: "home/sensor1/status"
    payload_available: "online"
    payload_not_available: "offline"
    json_attributes_topic: "home/sensor1/attributes"

Thats displayed in mqtt explorer
SessionLock
SessionUnlock
In your case.

1 Like

Thank you.

Update: Got it working. I edited the below code. Since I didn’t specifically ID the sensor in my Automation, not certain the sensor is actually needed. I’m not certain how they work :upside_down_face:

Here’s what I have so far… The Automation presumably does not fire, though nothing in the HA logs to indicate failure.

MQTT Explorer shows SessionUnlock for status, so I assume it would say SessionLock when session is locked.

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

Automation:

- id: '1649569660564'
  alias: PC Logoff
  description: ''
  trigger:
  - platform: mqtt
    topic: "iotlink/home/haf922/windows-monitor/status"
    payload: SessionLock
  condition: []
  action:
  - service: notify.mobile_app_sas22
    data:
      message: PC session locked
      title: Computer Activity
  - type: turn_off
    device_id: 49dc7efaec0941a1a9a0470e8424c3f7
    entity_id: light.office_desk_lamp
    domain: light
  mode: single
1 Like

Something I recognize now… my Automation is constantly firing. I realize this is due to IoT Link continually updating…

How could I structure my automation to only receive input from the sensor once until it changes? Is there some type of Template use that could work?

My initial thought was to have the Automation disable itself once activated, then have it re-enabled upon my Unlock Automation firing?

Try this Automation Trigger - Home Assistant

Update: I’m going to start a new thread with this topic.

https://community.home-assistant.io/t/iot-link-to-ha-in-json/411098

Yeah, I had looked at that page previously, but not sure it’s applicable in this scenario. For example, how do I split up an IoT Link topic that doesn’t reference anything in HA?