Set PC session state upon HA start

Not certain ‘Set’ is the proper word here. Through HASS.Agent and mqtt, I have a sensor that watches the state of my Windows session (Locked, Unlocked, and Unknown). If I log out/lock and back into my Windows session, HA properly displays such in my Dashboard. However, if I simply reboot HA while logged into my Windows session, HA shows the state ‘Unknown’ in Dashboard card.

Upon HA start, how could I check for the proper Windows session state and set the Dashboard card?

Sensor:

  - name: "PC1 - Session State"
    unique_id: pc1_session_state
    state_topic: "homeassistant/sensor/PC1/PC1_sessionstate/state"
    icon: mdi:account
    value_template: "{{ value }}"

Dashboard Card:

type: entity
name: Session Status
icon: mdi:microsoft-windows
entity: sensor.pc1_sessionstate

I assume you’ve got something running on your PC to send the MQTT message. If it sets the “retain” flag, the MQTT server will resend the message to any new subscribers. So when HA restarts and reconnects to MQTT, it will be sent the latest message and can update the state.

Ah, yes, thanks. I had such set w/ prev IoTLink, but now using HASS.Agent…
I’ll look into and if that works, I’ll mark this as SOLUTION.

I was not aware of HASS.Agent, but it looks interesting, so I’ve installed to check it out for myself. It looks like the default is to set the retain flag, but any sensors I add are being updated through the HA API, not MQTT. So I guess your setup is different.

I believe that’s the case, if you have ‘Local API’ enabled, otherwise, it should update via the default of MQTT.

How I’ve read it, in order to use Retained Messages, one must use it’s Satellite Service.

Actually, I now noticed the Retain Flag in MQTT config, so I have it enabled.

See this HASS.Agent GitHub Issue…

https://github.com/LAB02-Research/HASS.Agent/issues/239