Dynamic Groups Controller - Filter Expressions
-
Diving into Dynamic Groups and I am trying to create a
filter expression
for grouping battery devices that have a battery charge of between 11% and 35%. Thought this would be easy by setting my config to the followingenabled: true name: "Dynamic Group Controller" implementation: DynamicGroupController config: groups: "low_battery_35": select: - include_capability: battery_power filter_expression: entity.attributes.battery_power.level 0.11 < 0.35
However that didn't do the trick, I tried this as well no luck
- id: groups enabled: true name: "Dynamic Group Controller" implementation: DynamicGroupController config: groups: "low_battery_35": select: - include_capability: battery_power filter_expression: entity.attributes.battery_power.level < 0.35 entity.attributes.battery_power.level > 0.11
Is it even possible to have two filters for one group? I am trying to create 3 groups total one would notify me if a device falls below 35%, then another notification if it falls below 10%, then a final notification if the battery is dead. If I just leave the filter expression at
< 0.35
that device shows up in the 10% and 0% groups as well causing the device to show up multiple times in other groups. -
If only there was a file where the system left clues...
-
If only there was a file where the system left clues...
@toggledbits oops yes should've checked the logs first for some clues. Using this config below, there are no errors in the logs. However the group now shows devices that are below 35% battery and all devices above 11% battery, not inbetween 11% and 35% like I intend. Would I have to create a secondary group that filters the "low_battery_35" group and have the filter be for devices above 11% or is there a way to set the filter_expression to only show a the devices with a charge between 11% and 35% without needing to create a second group?
"low_battery_35": select: - include_capability: battery_power filter_expression: entity.attributes.battery_power.level < 0.35, entity.attributes.battery_power.level > 0.11
-
Actually, the fact that there's no error is a great surprise to me. It looks like you've walked into a YAML ambiguity. Half the online validators I throw your config into choke on it, and half don't. And it turns out, the YAML parser package I'm using in Reactor doesn't.
The problems in your config are two:
-
If you look carefully at the multi-line filter example in the docs, it begins
filter_expression: >
... and that>
is important. It's missing in your config. It tells YAML that a multi-line string follows, and it should be there to remove the aforementioned ambiguity in YAML syntax. -
If you fix #1, the expression you have then isn't going to do what you want it to do. You have given an expression list (two or more expressions separated by commas), and in that case, the result of the expression is the result of the last expression (only). That's defined by the expression parser library, and it's an important aspect of the syntax that allows complex expressions to be written. What you really want is to and these two expressions together using the
&&
operator. That would be:entity.attributes.battery_power.level < 0.35 && entity.attributes.battery_power.level > 0.11
You could also write that on one line. You could also use a temporary variable to make the expression run a little faster:
level = entity.attributes.battery_power.level, level < 0.35 && level > 0.11
That again is an expression list, and the result is that of the second expression (only) — boolean true if the level is between 0.11 and 0.35, and false otherwise.
-
-
Actually, the fact that there's no error is a great surprise to me. It looks like you've walked into a YAML ambiguity. Half the online validators I throw your config into choke on it, and half don't. And it turns out, the YAML parser package I'm using in Reactor doesn't.
The problems in your config are two:
-
If you look carefully at the multi-line filter example in the docs, it begins
filter_expression: >
... and that>
is important. It's missing in your config. It tells YAML that a multi-line string follows, and it should be there to remove the aforementioned ambiguity in YAML syntax. -
If you fix #1, the expression you have then isn't going to do what you want it to do. You have given an expression list (two or more expressions separated by commas), and in that case, the result of the expression is the result of the last expression (only). That's defined by the expression parser library, and it's an important aspect of the syntax that allows complex expressions to be written. What you really want is to and these two expressions together using the
&&
operator. That would be:entity.attributes.battery_power.level < 0.35 && entity.attributes.battery_power.level > 0.11
You could also write that on one line. You could also use a temporary variable to make the expression run a little faster:
level = entity.attributes.battery_power.level, level < 0.35 && level > 0.11
That again is an expression list, and the result is that of the second expression (only) — boolean true if the level is between 0.11 and 0.35, and false otherwise.
@toggledbits That seems to have done the trick! I originally removed the
>
because I thought without out Reactor would treat each new line as a new entry but I see that it is the total opposite. Thanks for the help! -
-
T toggledbits locked this topic on
-
T toggledbits referenced this topic on