Looking for template sensor to set a number / count how many binary sensors in a group are ON

looking for a template to count how many people are in a room based off of BLE TRACKER binary sensors. doing it with a number and a automation to adjust the number input but gets off track. found a few template sensors but couldn’t get them to work with my configuration. I was using espresense count function flawlessly, but like the ability to customize with esphome and trying to combine everying to one esp32 per room with maybe 1 esp8266 (way to many wifi devices in my house, get stuff out the cloud). Hx711 bed Weight sensor, with smart scale to track bed_weight and know people in the room vs in bed to turn the light off if room = bed, turn on the light when I get out of bed (under bed lighting / indirect) and turn off the light when I leave the room and my girlfriend is still asleep, but repeat when she gets outta bed. something like from the post below

I’m not clear on what your configuration is, but if you’re simply trying to count the number of binary sensors in a group that are turned on, you can use this:

{{ expand('binary_sensor.my_binary_sensor_group') | selectattr('state','eq','on') | list | count }}

Or get a list of the entities that are on:

{{ expand('binary_sensor.my_binary_sensor_group') | selectattr('state','eq','on') | map(attribute='entity_id') | list }}

Play around with those in the “template” portion of developer tools until you get what you need.

That did the trick. thank you, thank you… going on a year with home assistance but still lot to learn. now I have a binary_sensor.bedroom_occupancy which gives me the numb of people in the room. and i have a automation that takes the weight of a hx711 if between 90 to 200 lbs 1 person in bed 200+, 2 people in bed, to which it sets input_number.bed_numb. Now i’m looking for a good way to compare and if more people in the room then in bed, and when the binary_sensor.bedroom_occupancy = input_number.bed_numb. and i guess get a count to on if only if the binary_sensor.bedroom_occupancy is higher than the input_number.bed_numb. i was use a range group sensor but it doesn’t track above or below, only the difference, and if watch’s ble binary sensor dropped off and dropped room number to 1 but bed 2, would get a 1, only want a trigger if the bedroom_occupancy is higher than the bed_numb

I’d be glad to help you some more but I can’t follow at all what you’re trying to say. A binary _sensor is only on or off (hence “binary”) and therefore can’t equal the number of people in a room. I don’t know what a range group sensor is either. If you’re able to spend more effort explaining your objective I’d be glad to help out.

yah binary sensor is wrong. sensor.bedroom_occupancy vs input_number.bed_numb is what I’m looking for. I had them in a group and doing Statistical range to get the difference but doesnt factor which one is greater / give me a negative number

Are you just looking for a template to calculate the difference between the two sensors?

{{ states('sensor.bedroom_occupancy') | float(0) - states('input_number.bed_numb') | float(0) }}

Take care to think about what value you’d like to assume when either entity is unavailable or unknown and replace the zero in the float filter with that value.

BIG THANKS. everything working as I envisioned.