Fail2Ban integration - creating count metric of current / total bans for sensors

I have Fail2Ban configured on my Pi, with various services, including Home Assistant, and SWAG etc all running as containers.

I’ve configured the Fail2Ban integration to consume my frontend SWAG log file and all works with the sensor state showing the most recent banned ip, though I’d like to show an aggregated sensor / count value of the current bans / total bans.

Ideally, aggregated count of all the sensor.fail2ban_* jails for attributes current_bans and total_bans.

I’m guessing this could be done as a template sensor with some state logic but appreciate any guidance that could be provided on the best approach…

Update, I’ve got this working as expected for 1 of the sensors.

Still ideally I’d like these metrics to include the other fail2ban_* sensors (for the other jails):

template:
        - sensor:
            - name: "Fail2Ban Current Banned"
              state: "{{states.sensor.fail2ban_nginx_unauthorized.attributes.current_bans | count }}"
              state_class: measurement
        - sensor:
            - name: "Fail2Ban Total Banned"
              state: "{{states.sensor.fail2ban_nginx_unauthorized.attributes.total_bans | count }}"
              state_class: measurement

Still playing around with this - in addition I’m trying to change this so it’s not returning as a string, and instead an int (the quotes around the output are somewhat annoying!)

I’ve tried | int after count and no success

Hi,

From what I understand, in the template editor, you can remove the quotes surrounding the whole state_attr function and this should stop it from being a string. The count should return a number but the quotes are converting this to a string after.