Linux desktop activity state tracking via MQTT

Background: I have a linux tower PC in my office that also has a Mysa smart thermostat in it, and I wanted Home Assistant to know when I’m actively using that computer so it can know I’m in the room and adjust the room temperature automatically.

To make that work I wrote a Python script I can run from my user account on the workstation that subscribes to the screensaver DBus interface and updates an MQTT topic regarding my activity status. GitHub - bedaro/idle-tracker: MQTT Linux Desktop Idle Status Publisher So status is published to /<host>/user/<username>/status and takes either the value “active” or “inactive”

If anyone is interested in publishing other Linux system information to MQTT, I wrote the code in a way that’s meant to be flexible so it can relatively easily be adapted to other data.

Then in my HA configuration.yaml I can define a binary sensor linked to the MQTT topic:

mqtt:
  binary_sensor:
    - state_topic: <host>/user/<username>/status
      name: Ben using workstation
      icon: mdi:account
      unique_id: ben_using_workstation
      payload_on: active
      payload_off: inactive
      expire_after: 600

…and now Home Assistant knows about it! The rest is all in a (complicated) Node-RED flow.

1 Like