Variable is not being set, but why?

I think I’m missing something obvious.

I have a binary_sensor template that checks if any sensors are not in their closed state. It does this by seeing if the count of open sensors is true (non-zero).

But, I wanted to keep the count in a variable for use later when setting an attribute and for setting the state. But, the “is_open_count” variable doesn’t seem to get set, and I’m not sure why.

Could someone be so kind as to explain why this isn’t setting the “is_open_count” variable?

- binary_sensor:
    - name: Garage Closed
      device_class: garage_door
      unique_id: garage_door_opened_group


      variables:
        # Why is this not getting set to the count?
        is_open_count: |
          {{
            label_entities("garage_sensor")
            | reject('is_state', ['off' , 'closed'])
            | list | count
          }}

        # This works:
        another_count: |
          {{
            ['one','two'] | count
          }}

      # This is setting the state correctly
      state: |
          {{
            label_entities("garage_sensor")
            | reject('is_state', ['off' , 'closed'])
            | list | count
          }}
      attributes:

        # Why no count here?
        is_open_count: "{{ is_open_count }}"

        # This works
        count: |
          {{
            label_entities("garage_sensor")
            | reject('is_state', ['off' , 'closed'])
            | list | count
          }}
        another_count: "{{ another_count }}"

Here’s the two states (“off” and “on”):

Note that the state is “on” now, and “count” has changed, but not “is_open_count”:

Template - Home Assistant doesn’t list anything about supporting a variables: clause for template sensor or template binary_sensor?

Where are you getting the idea it’s supported?

Note all the examples build the state inside the state or attribute and none seem to store a variable?

Ah, you are right.

Where I’ve used them before is with triggers in my template_sensors.yaml file, and I just did a bit of copy-n-paste where I didn’t need the trigger.

I knew I was missing something – but tricked myself seeing some of the variables seemly working.

Thanks,

1 Like

Variables may be defined only for trigger-based sensors inside an optional “action” section - and then used below:

template:
  - trigger:
      - platform: ...
        ...
    action:
      - variables:
          ABC: ...
    binary_sensor:
      - name: xxx
        ...
        state: ... use here your variable
        attributes:
          yyy: ... use here your variable
        availability: ... use here your variable
1 Like

Why not use a group?

No real reason. I use groups already. I thought it would be interesting to use labels and see if I like that UI better. These are entities that belong to multiple groups.

Is there a reason not to use the labels approach?

Thanks

FYI variables are available for template entities the way you have it written, but it only works with blueprints. It’s new as of 2034.11

Waiting 10 years might be a bit too much. :rofl:

1 Like