Help counting items in list

I’ve spent days reading the templating documentation and I just need a little extra help to understand something about the syntax

I have written a blueprint where the user can select any number of input_booleans as triggers, because I have used multiple: true. The field is called trigger_feeds. According to documentation I understand that trigger_feeds becomes a list because I used multiple: true

In my value_template in my condition, what should I put to get a count of how many input_booleans the user selected in trigger_feeds? I can’t believe I’m having to ask this, getting a list length seems such a simple operation usually. :smiley:

Some template that includes 'entity' | list | count

1 Like

So I now have:
value_template: “{{ ‘trigger_feeds’ | length | count == 3 }}”

I happen to know the user (me) selected three input_booleans which should in the list ‘trigger_feeds’ but the expression is still evaluating to false

When you put it in quotes like that it is no longer a variable. Assuming you have properly converted the input to a variable you would use:

value_template: "{{ trigger_feeds | count == 3 }}"

If you are new to working with Blueprints and Templates See Also:
Blueprints: Get your !input value into a template

That’s what I’d have thought but I wanted to try Sir Goodenough’s suggestion as posted as I’d been having no luck using my common sense lol. Tried again without quotes - still false.

    value_template: "{{ trigger_feeds | length | count == 3 }}"

Oh you didn’t include the length I’ll omit that too

Nope still evaluating false. To rule out errors in the rest of the file structure I did try “{{ 1 == 1 }}” which evaluated true.

The filters length and count do the exact same thing… using them multiple times in sequence is invalid because an integer doesn’t have a length as far as Jinja is concerned.

Post the full YAML.

I need to go to bed now, tomorrow I was planning to get some more debugging in and find out exactly what it does think the count is. Thanks for your help so far

blueprint:
  name: ijMotionFlexiGroups
  description: 04 Turn a Boolean Helper on if a certain threshold of sensors is crossed
  domain: automation
  author: Inge Jones

  input:
    trigger_feeds:
      name: Sensor Triggers
      description: This sensor will be synchronized with the light.
      selector:
        entity:
          domain: 
            - input_boolean
          multiple: true

    min_feeds:
      name: Minimum Triggers
      description:  How many sensors will have to be active at the same time to set group to active
      default: 1
      selector: 
        number:
          min: 1
          max: 100

    target_toggle:
      name: Boolean Helper
      description: The boolean helper to use as indicator.
      selector:
        target:
          entity:
            - domain: input_boolean

trigger:
  - platform: state
    entity_id: !input trigger_feeds

action:
  - choose:
    - conditions:
      - condition: template
        value_template: "{{ trigger_feeds | count == 3 }}"

      sequence:
        - service: input_boolean.turn_on
          target: !input target_toggle

    default:
      #- service: input_boolean.turn_off
      #  target: !input target_toggle
      - service: input_boolean.turn_on
        target: 
          entity_id: input_boolean.DEBUGGINGBOOL



You must define your input as a YAML variable in order for it to be available for templating.

Blueprint with YAML variable
blueprint:
  name: ijMotionFlexiGroups
  description: 04 Turn a Boolean Helper on if a certain threshold of sensors is crossed
  domain: automation
  author: Inge Jones

  input:
    trigger_feeds:
      name: Sensor Triggers
      description: This sensor will be synchronized with the light.
      selector:
        entity:
          domain: 
            - input_boolean
          multiple: true

    min_feeds:
      name: Minimum Triggers
      description:  How many sensors will have to be active at the same time to set group to active
      default: 1
      selector: 
        number:
          min: 1
          max: 100

    target_toggle:
      name: Boolean Helper
      description: The boolean helper to use as indicator.
      selector:
        target:
          entity:
            - domain: input_boolean

trigger:
  - platform: state
    entity_id: !input trigger_feeds

variables:
  t_feeds: !input trigger_feeds

action:
  - choose:
    - conditions:
      - condition: template
        value_template: "{{ t_feeds | count == 3 }}"

      sequence:
        - service: input_boolean.turn_on
          target: !input target_toggle

    default:
      #- service: input_boolean.turn_off
      #  target: !input target_toggle
      - service: input_boolean.turn_on
        target: 
          entity_id: input_boolean.DEBUGGINGBOOL

However, I don’t think that condition is actually what you want… as shown, it chooses an action based on how many trigger entities are defined. Was that your goal?

If not, explain what you are trying to achieve, we can probably point you in the right direction.

2 Likes

No of course I won’t be using that condition in the final blueprint, that’s what it had come down to after all attempts at the desired condition had failed and I was trying to see if I could get anything about that list working at all. The desired condition will count the booleans in the list that are on or true and see if they meet the number the user had selected as being the minimum number to turn on target toggle. It’s kind of like sensor groups but without the all or nothing. I expect someone’s already done it but I wanted to start somewhere. Ok so I need to read how to define the variable for use properly. Thanks very much hopefully I will be able to solve it now!

1 Like

Might help with your reading list.

Yep that did it - such a simple fix yet I didn’t notice that explained in any of the reading I did. Or I may have seen it and missed its significance. @Sir_Goodenough I didn’t come across your guide. That would have helped.

Thank you both of you for your time.

My eventual condition statement is

value_template: "{{ (expand(t_feeds)|selectattr('state', 'eq', 'on')|list|count) >= m_feeds }}"

Now in order for the automation to be actually useful I need to create a virtual motion sensor rather than my test booleans!