Counting binary sensors in a specific state

Hello all,

I have a day-night sensor wired into a Shelly2.5 which is wired directly to two security lights. Using the input switch, binary_sensor.security_day_night, I have created various other automations to turn on lights, and generally prepare the house for evening.

One issue is that because of the wiring, which I’m not able to change immediately, if the security lights are turned off at the switch, the sensor and Shelly both lose power and thus a number of my automations are never activated.

I additionally have about 10 Unifi Protect cameras in HA, and each of these cameras (since a recent update?) now have a binary_sensor.is_dark_* entity. I could conceivably wire up my automations to one of these camera attributes quite easily. However, individual cameras can fail and thus the automations are reduced to a single point of failure.

I have created a group of these camera sensors and included the day-night sensor in this group. Using the group is possible but groups are either turned on by “ONE” or “ALL”. With these two states, the system is either prone to increased false positives or at least subject to the most sensitive reading. Requiring “ALL” again increases issues of failure.

I would thus like to consider it “dark” only when, for example, 33% or more of the sensors in the group report that it is dark. This allows for some to be sensitive, and for others to fail without having overly serious effects on the automations that are linked to the arrival of darkness.

How can I do this? I am very new at Home Assistant! While I’ve found articles within this forum which supposedly answer my question, I’ve been unable to get anything working. My guess is that many of the configuration options and YAML code provided is simply not getting to the right place. If you could please keep in mind that I may need highly simplified instructions, that would be appreciated.

Put all your binary_sensors you want to count into a group (in the example below I picked group.is_dark)

Then add this into your config:

sensors:
- platform: template
    sensors:
      is_dark_count:
        value_template: >-
           {{ states | selectattr('entity_id','in',state_attr('group.is_dark','entity_id')) | selectattr('state','eq','on') | list | count | int}}

Then you should have a sensor.is_dark_count which will tell you how many of that group are on. You can then specify in your automation what your threshold is:

automation:
  trigger:
    - platform: numeric_state
      entity_id: sensor.is_dark_count
      above: 3
1 Like

I would go about it a different way than the previous response. You are asking a binary question, is it dark? yes/no. So you should create a state-based template binary sensor.

template:
  - binary_sensor:
      - name: "Is it Dark"
        state: >
          {% set reporting_dark = expand('group.is_dark') | selectattr('state', 'eq', 'on') | list | count | float(0) %}
          {% set working_sensors = expand('group.is_dark') | rejectattr('state', 'in', ('unknown', 'unavailable')) | list | count | float(0) %}
          {{ reporting_dark / working_sensors >= 0.33  }} 

This will create an entity binary_sensor.is_it_dark which will have a state of on when at least 33% of the working sensors from your group are reporting that it is dark.

Notes:

  1. You did not give the name of the sensor group you are using so I used the same one as the previous respondant.
  2. You did not tell us whether the binary_sensor.is_dark_* sensors report ‘on’ or ‘off’ when it is dark… The template above assumes ‘on’ means it is dark.
  3. You may notice some differences in the template format and method I have used compared to the previous respondant. The template sensor format I have posted it the current recommended format. While the other (legacy) format will work, it is no longer recommended. Similarly, the previous response uses an open ended state object in his template. This is also not recommended and can contribute to lag because the template will re-render every time any entity in your Home Assistant instance updates.