Trying to count number of MQTT devices with low linkquality

Hi,

I cannot get this sensor to count correctly. I have a feeling it is because the state isn’t a number (even though it looks like one).

It currently matches/counts 4 binary_sensors, only 1 of which actually has a state <=50.

Any help would be appreciated. I am not great at using filters (all my knowledge has been from examples others have written). I have tied the jinja2 website to learn, but that isn’t easy reading.

- name: "count of MQTT low link quality"
  icon: mdi:signal
  unique_id: count_of_mqtt_low_linkquality
  unit_of_measurement: "lqi"
  state: >
    {{ states.binary_sensor
      | selectattr('name','match','.*linkquality')
      | selectattr('state','<=','50')
      | list
      | count
    }}

If the link state is reporting a numeric value, it won’t be a binary_sensor. It should just be sensor. Mine show up like sensor.front_door_linkquality.

1 Like

ugh, sorry - I copied/pasted the yaml file version which I had abandoned halfway through to use dev tools > sensor for my proofing. The version I copied in error was a copy/paste from my ‘MQTT update available’ sensor (hence binary_sensor).

Here is the one (from dev tools > template) that is displaying the problem.

    {{ states.sensor
      | selectattr('name','match','.*linkquality')
      | selectattr('state','<=','50')
      | list
      | count
    }}

OK, for those that may want to know this in the future, the following worked:

{{ states.sensor
  | selectattr('entity_id','match','.*linkquality')
  | map(attribute='state')
  | map('int')
  | reject('>',50)
  | list
}}

…currently returning: [36, 12, 42]

from: ['66', '99', '66', '57', '60', '108', '36', '123', '126', '72', '96', '81', '99', '81', '123', '78', '60', '57', '60', '57', '51', '57', '12', '57', '57', '63', '60', '60', '63', '60', '57', '42', '54', '66', '114', '126']

…just needed to add |count at the end to get what I wanted:

{{ states.sensor
  | selectattr('entity_id','match','.*linkquality')
  | map(attribute='state')
  | map('int')
  | reject('>',50)
  | list
  | count
}}

…returning: 3

1 Like