Passing a variable in automation (issue with a sensor collection)

Hi, I have some problem with passing a variable in automation. I am trying to pass a collection of sensors but it failed. It is a standard enumeration of entities with battery_class and everything works great in the template editor, but I don’t know why it doesn’t work in automation. I must have missed something. Can anyone advise?

This is the part of the automation I am trying to pass the {{ monitored }} variable. The final notification displays two different values: variable: 4565, template: 18.

- alias: Battery Check
  id: battery_check
  trigger:
  - platform: time
    at: "10:00:00"
  variables:
    threshold: >
      {{ 20 }}  
    exlude: >
      {{ expand('group.sensors_exclude_battery_check') | map(attribute='entity_id') | list }}
    monitored: >
      {{ states.sensor
        | rejectattr('attributes.device_class', 'undefined') 
        | selectattr('attributes.device_class', 'eq', 'battery')
        | rejectattr('entity_id', 'in', exlude)
        | list }}
    unavailable: >
      {{ expand(monitored) 
        | selectattr('state', 'in', ['unavailable', 'unknown', 'none']) 
        | map(attribute='entity_id') 
        | list }}
    low_battery: >
      {{ expand(monitored) 
        | rejectattr('entity_id', 'in', unavailable)
        | map(attribute='state')
        | map('int') 
        | select('<=', threshold) 
        | map('string') 
        | list }}
    pass: >
      {{ (low_battery | count > 0) or (unavailable | count > 0) }}
  condition:
  - condition: template
    value_template: "{{ pass }}"
  action:
    - service: notify.hass_agent_hugo
      data:
        message: >
          variable: {{ monitored | count }}, 
          template: {{ states.sensor
            | rejectattr('attributes.device_class', 'undefined') 
            | selectattr('attributes.device_class', 'eq', 'battery')
            | rejectattr('entity_id', 'in', exlude)
            | list | count }}

If I tried to pass the variable monitored as a collection of entities by adding a filter | map(attribute='entity_id'), then passing the variable was successful. I do not understand why.

Example in the template editor (works like a charm):

{% set threshold = 15 %}

{% set exclude = expand('group.sensors_exclude_battery_check') 
  | map(attribute='entity_id') 
  | list %}

{% set monitored = states.sensor
  | rejectattr('attributes.device_class', 'undefined') 
  | selectattr('attributes.device_class', 'eq', 'battery')
  | rejectattr('entity_id', 'in', exclude)
  | list %}

{% set unavailable = expand(monitored) 
  | selectattr('state', 'in', ['unavailable', 'unknown', 'none']) 
  | map(attribute='entity_id') 
  | list %}

{% set low_battery = expand(monitored) 
  | rejectattr('entity_id', 'in', unavailable)
  | map(attribute='state')
  | map('int') 
  | select('<=', threshold) 
  | map('string') 
  | list %}

Low Battery:
------------
{% for state in monitored | selectattr('state', 'in', low_battery)  -%}
  {{ state.name + ": " + state.state + " %" }}
{%- endfor %}

Empty battery:
--------------
{{ unavailable | join('\n') }}

DEBUG INFO ---> 
threshold: {{ threshold }}
excluded sensors: {{ exclude | count }}
monitored sensors: {{ monitored | count }}
unavailable sensors: {{ unavailable | count }}
low battery sensors: {{ low_battery | count }}

Template editor (printed result):

Low Battery:
------------
Kids room sensor lightlevel Battery: 15 %

Empty battery:
--------------
sensor.mi_magic_cube_battery_level
sensor.mi_smart_switch_battery_level
sensor.sensor_door_battery_level
sensor.sensor_mrazak_battery_level

DEBUG INFO ---> 
threshold: 15
excluded sensors: 5
monitored sensors: 18
unavailable sensors: 4
low battery sensors: 1