Counting steam players

I’m trying to create a template sensor that counts how many of my friends are playing games on Steam.

I’ve come up with this:

count_steam_gamers:
  value_template: "{{ states.sensor | selectattr('attributes.game') | list | count  }}"
  icon_template: mdi:steam
  friendly_name: 'Steam'
  unit_of_measurement: "Player(s)"

The steam sensors only show the attribute ‘game’ when they’re playing a game. Otherwise the game attribute is missing.

  • Template variable warning: ‘mappingproxy object’ has no attribute ‘game’ when rendering ‘{{ states.sensor | selectattr(‘attributes.game’) | list | count }}’

This error shows up, and while the sensor does work, it spams the log about 80.000 times in only a couple of minutes.

When I edit out this template sensor, the load on my system seems much lower.

Is there any way you can think of to get this working without it looping and spamming the log constantly?