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!