Calculating the number of entities whose state is "on" as a percentage of all entities,

I have 3 input_booleans defined. Each has a custom_button on the UI that changes color based upon state.

I would like to create a sensor that would look at all 3 input_booleans and return a value that represents a percentage of those inputs whose state is “on”.

If two are on, it would report 66%, etc.

Likewise, a sensor that could simply return the count of input_booleans that are “on”.

So, I need to be able to create a total count of specific input_booleans as a sensor value, then a total count of specific input_booleans that are “on”, and run some calculations.

Any whizzards out there who can help me with a template_sensor to achieve this?

value_template: "{{ ( [states.input_boolean.larry, states.input_boolean.curley, ]|selectattr('state', 'eq', 'on') | list | count * 33.333 )|round(0) }}"

and to be more generic so that if you add more booleans you could calculate how many total you have and then use that.

{% set members = [states.input_boolean.larry, states.input_boolean.curley, ] | list %}
{% set total = members | count %}
{{ ((( members |selectattr('state', 'eq', 'on') | list | count) / total ) * 100)| round(0)}}
1 Like

Here’s what I ended up with which searches through the input_booleans for specific ones so I don’t have to name them individually and so new ones will automatically show up when I name them. I first create a list of those, then a list of those that are on, and those that are off, then a total count of all, and then some math to create a value to be used as a sensor, but there’s a problem at the bottom.

//Create a list of all input_booleans that contain "taco"

{% set boolean_list = states.input_boolean|selectattr('object_id', 'search', 'taco*')|list %}

// From that list, create a list of one state (off)

{{ boolean_list|selectattr('state', 'equalto', 'off') | list | length }}

// And a list of 'on' states

{{ boolean_list|selectattr('state', 'equalto', 'on') | list | length }}

// And a count of all in boolean_list.

{{ boolean_list|count}}

Then do math:

{% set percent_on = (boolean_list|selectattr('state', 'equalto', 'on') | list | length | int) / (boolean_list|count) %}

{{percent_off * 100 }}

In my result, I have 3 items, two of which are off and one is on. I’m getting a result of 33.3333333.

QUESTION: What do I need to change in the last line to eliminate the repeating 3’s? I’ve tried | round() but it doesn’t work. I suspect it’s because of the format of the number, but I can’t seem to figure out the filter. Thanks!

I think I got it…

I changed the last line to {{percent_on | float | round(2) * 100 }} and I condensed the entire lot to the following:

//Find all input booleans with the word taco in it
{% set boolean_list = states.input_boolean|selectattr('object_id', 'search', 'taco*')|list %}

//Divide the ones in that list whose state is 'on' by the total count of that list
{% set percent_on = (boolean_list|selectattr('state', 'equalto', 'on') | list | length ) / (boolean_list|count) %}

//Display the result as an adjusted value to represent a percent value
{{percent_on | float | round(2) * 100 }}

Is there a more elegant way to achieve this?