Skip to content

Added options to Lovelace evaluate filter#5694

Merged
bramkragten merged 3 commits into
home-assistant:devfrom
jayknott:dev
May 6, 2020
Merged

Added options to Lovelace evaluate filter#5694
bramkragten merged 3 commits into
home-assistant:devfrom
jayknott:dev

Conversation

@jayknott
Copy link
Copy Markdown
Contributor

@jayknott jayknott commented May 1, 2020

Proposed change

Adds the ability to filter arrays, JSON objects, and sub-strings in Lovelace cards that use the evaluate-filter common function. This is executed by adding "in" and "not in" to the list of allowed operators and testing if the state or state attribute is an array. If it is an array, it tests if the array includes a value of (or does not include a value of for "not in") the filter.value parameter. If the state or state.attribute is a string it will see if the filter.value is a sub-string of (contains) the state or state.attribute. The "regex" operator is modified to evaluate if the state or state.attribute is an array or JSON object and then converts it to a string using JSON.stringify() so regex can be used to evaluate it. The "regex" operator is unchanged for all other types.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example configuration

# Value is in an array attribute
- type: entity-filter
  entities:
    - media_player.sonos_1
    - media_player.sonos_2
    - media_player.sonos_3
  state_filter:
    - attribute: sonos_group
      operator: in
      value: media_player.sonos_1

# Value is not in an array attribute
- type: entity-filter
  entities:
    - media_player.sonos_1
    - media_player.sonos_2
    - media_player.sonos_3
  state_filter:
    - attribute: sonos_group
      operator: not in
      value: media_player.sonos_1

# Regex evaluation on array attribute
- type: entity-filter
  entities:
    - media_player.sonos_1
    - media_player.sonos_2
    - media_player.sonos_3
  state_filter:
    - attribute: sonos_group
      operator: regex
      value: '.*sonos_1'

# Regex evaluation on JSON/object attribute
- type: entity-filter
  entities:
    - weather.city_1
    - weather.city_2
  state_filter:
    - attribute: forecast
      operator: regex
      value: '"condition": "rainy"'

Additional information

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

@homeassistant
Copy link
Copy Markdown
Contributor

Hi @jayknott,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

Comment thread src/panels/lovelace/common/evaluate-filter.ts Outdated
Comment thread src/panels/lovelace/common/evaluate-filter.ts Outdated
@jayknott jayknott requested a review from bramkragten May 5, 2020 21:10
@bramkragten bramkragten merged commit c631554 into home-assistant:dev May 6, 2020
@bramkragten bramkragten mentioned this pull request May 9, 2020
@lock lock Bot locked and limited conversation to collaborators May 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants