ELI5
The ELI5 of this script is: the current target selector only lets you say the word "or" when using labels. This script let's you say the words "and," "not," and "or" when using labels. An analogy would be if you were looking for a "blue" "long-sleeve" "shirt" in your closet. The current target selector would give you all your "blue" clothes (shirts, pants, scarves, etc.), all of your "long-sleeve" clothes (shirts, sweaters, coats, etc.), and all of your "shirt"s (t-shirts, button downs, a-shirts, etc.). Whereas this script would actually let you get your "blue" "long-sleeve" "shirt"s.
I got frustrated with labels not being able to be used to filter different semantic categories in the target selector, and instead only being able to add more things the more labels you add. The only real workaround is to template out the set operations you want (e.g. devices with all of the labels, or entities with labels "foo" and "bar", but not "baz"). After the third or fourth time writing a template, I wrote a script to do the work, and have been using it since. It seems to work pretty well, so I've made it into a script blueprint you can import and start using immediately:
This script basically has three features:
- Finding areas/devices/entities with label foo OR label bar.
- This is the only behavior of the current target selector.
- Finding areas/devices/entities with label foo AND label bar.
- This is new behavior over the current target selector.
- Finding areas/devices/entities with label foo but NOT label bar.
- This is new behavior over the current target selector.
That's it, nothing fancy. You could definitely do this with a template. But if you're like me, writing Jinja in YAML makes you want to never write code for the rest of your life
.
A non-exhaustive list of threads where this script could be of use:
- Label_entities(): allow multiple labels selection (intersection)
- Select match all/any when multiple labels selected in call service
- WTH Labels: Optional AND-condition for labels in lists and history
- How to automate a target "label_a AND label_b"
- Script to turn off lights based on area and labels does not work as expected