Simple template not working

I have HASS.Agent installed on my computer. I want to use the lastsystemstatechange sensor to know when the computer gets locked and unlocked.

I have this in my configuration for a custom binary sensor:

template:
  - binary_sensor:
      - name: "Computer Session Locked"
        unique_id: computer_session_locked_status # Ensure this is unique
        device_class: lock
        state: >
          {{ is_state('sensor.computer_lastsystemstatechange', 'SessionLocked') }}
        attributes:
          last_system_state: "{{ states('sensor.computer_lastsystemstatechange') }}"

But even when this evaluates to false, the sensor always shows Locked. I tested evaluating {{ is_state(‘sensor.computer_lastsystemstatechange’, ‘SessionLocked’) }} in the dev tools and it evaluate to false, so I guess it should be Unlocked…

We can even see here the last system state to be SessionUnlock.

Then I tried this to update it periodically, still same problem:

template:
  - trigger:
    - platform: time_pattern
      seconds: "/30"  # checks every 30s if you're still idle
    - platform: state
      entity_id: sensor.computer_lastsystemstatechange
    binary_sensor:
      - name: "Computer Session Locked"
        unique_id: computer_session_locked_status # Ensure this is unique
        device_class: lock
        state: >
          {{ is_state('sensor.computer_lastsystemstatechange', 'SessionLocked') }}
        attributes:
          last_system_state: "{{ states('sensor.computer_lastsystemstatechange') }}"

I do a quick reload after I change the config. Also tried a full reload.

A binary_sensor with device_class: lock will show as unlocked when the state is on. This can be seen here:

Edit: whenever you are debugging template sensors, make sure you utilize developer tools → states, instead of looking at various screens in the UI. The “states” page will show you the actual state of the entity instead of the “translated” state that the frontend uses. If you look at your new template sensor on that page, you will realize your template is doing what you expect.

Thanks for the quick reponse, unfortunately even when I change my condition to “not” state: '{{ not is_state("sensor.computer_lastsystemstatechange", "SessionLocked") }}', the binary_sensor doesn’t update when lastsystemstatechange changes to SessionLocked.

When looking in developer tools for the history of state changes for computer_lastsystemstatechange, here is what I get where the yellow is when I locked my computer.

Now the state is stuck at Unlocked (since I negate it in the template), but it never changes:

I also tried to keep it locked for more than a minute or two, but still doesn’t change.

You need to be looking at the states in developer tools → states. You can’t trust the state names you see in the history viewer.

Take a screenshot of the states page showing the state of the sensor.computer_lastsystemstatechange entity.

My prediction: the locked state is SessionLock and not SessionLocked like you are using in your template.

1 Like

This seems to work now:

- trigger:
  - platform: time_pattern
    seconds: "/30"  # checks every 30s if you're still idle
  - platform: state
    entity_id: sensor.computer_lastsystemstatechange
  binary_sensor:
    - name: "Computer Session Locked"
      unique_id: computer_session_locked_status # Ensure this is unique
      device_class: lock
      state: '{{ states("sensor.computer_lastsystemstatechange") != "SessionLock" }}'
      attributes:
        last_system_state: '{{ states("sensor.computer_lastsystemstatechange") }}'

Thanks for your help!