Template value to match a string in an entity attribute

I’m trying to match a string in an entity attribute using a template.

I’ve tried a few combinations but not getting a match.

My entity looks like this:

My automation:

alias: Network - Batchelor(ette) Night On/Off
description: ''
trigger:
  - platform: time_pattern
    minutes: /1
condition:
  - condition: template
    value_template: >
      {{ 'The Bachelorette' in
      states.sensor.sonarr_upcoming_media.attributes.data }}
action:
  - service: input_boolean.turn_on
    target:
      entity_id: input_boolean.bachelor_night
mode: parallel
max: 10

I’ve also tried:

{{ 'The Bachelorette' in state_attr(“sensor.sonarr_upcoming_media”, “data”) }}

Neither result in the automation being triggered.

Are you just trying to trigger if “The Bachelorette” is in the title field of any of attributes there? If so, try this:

{{ state_attr('sensor.sonarr_upcoming_media', 'data')|selectattr('title', '==', 'The Bachelorette')|list|length > 0 }}

You also had some wonky quotes characters in there. Thankfully, this evaluates to “false” for me :slight_smile:

1 Like

In addition, there’s probably no need to poll the entity’s data attribute every minute. Just create a State Trigger to monitor the data attribute for changes.

  trigger:
    - platform: state
      entity_id: sensor.sonarr_upcoming_media
      attribute: data

True, or just use the above as a template trigger.

Thanks mates, that did the trick.

Yeah, I plan to refine the trigger. The time pattern is more for testing purposes.

Cheers!

1 Like

Even better! :+1:

There’s also the opportunity to use the new match or search functions to permit more flexible string-matching (i.e. substring matching).

Bonus points! Possible to do an if/else for an action?

For instance, if conditions met then set input_boolean.bachelor_night = true else input_boolean.bachelor_night = false?

The answer to your question is yes but you set an input_boolean’s state to on or off, not true or false.

Simple example where the input_boolean is turned on if a sensor’s value exceeds 10. Otherwise it’s turned off.

action:
- service: "input_boolean.turn_{{ 'on' if states('sensor.whatever') | int > 10 else 'off' }}"
  target:
    entity_id: input_boolean.bachelor_night

Hmm…

'data')|selectattr('title', '==', 'The Bachelorette')|list|length > 0

works to find if that string is present.

But for some reason != as in:

'data')|selectattr('title', '!=', 'The Bachelorette')|list|length > 0

still evaluates as true when the string is present. Basically I need another automation to turn off the input boolean if that string isn’t present.

Not sure why you quoted a fraction of one of my posts then commented on a template I didn’t provide. :man_shrugging:

Nevertheless, the modified version you posted works exactly the way it’s written (i.e. the result is correct), just not the way you think it’s supposed to work.

It selected everything that did not have that value (The Bachelorette). Obviously there are many entries without that value so the count is greater than 0 and the template evaluates to true.

The original template correctly reports true/false if the value is present/missing.

I was replying to rrcolemans post that apparently quoted a part of yours.

But I got what I was after with:

action:
  - service: >-
      input_boolean.turn_{{ 'on' if (state_attr('sensor.sonarr_upcoming_media',
      'data')|selectattr('title', '==', 'The Bachelorette')|list|length > 0 or state_attr('sensor.sonarr_upcoming_media',
      'data')|selectattr('title', '==', 'The Bachelor')|list|length > 0) and
      is_state('sensor.day_of_week', 'Tuesday') else 'off'}}
    target:
      entity_id: input_boolean.bachelor_night

Cheers!

That’s a perfect application for the use of match.

action:
  - service: >
      input_boolean.turn_{{ 'on' if (state_attr('sensor.sonarr_upcoming_media', 'data')
        | selectattr('title', 'match', 'The Bachelor') | list | length > 0  and
        is_state('sensor.day_of_week', 'Tuesday') else 'off' }}
    target:
      entity_id: input_boolean.bachelor_night

It will match any string that begins with The Batchelor (meaning it will also match The Bachelorette).

Perfect! I love simpler. Thanks again!

Circling back on this. After some time I wanted to start using this again but now I’m getting this error when I plop that template into the checker:

UndefinedError: 'dict object' has no attribute 'title'

Here’s the sensor value:

data: 
- title_default: $title
  line1_default: $episode
  line2_default: $release
  line3_default: $rating - $runtime
  line4_default: $number - $studio
  icon: mdi:arrow-down-bold
- airdate: '2022-07-19T00:00:00Z'
  release: $day, $time
  flag: false
  title: The Bachelorette
  episode: Episode 2
  number: S19E02
  runtime: 85
  studio: ABC (US)
  rating: ★ 6.3
  genres: Drama, Game Show, Reality, Romance
  poster: https://artworks.thetvdb.com/banners/posters/71187-8_t.jpg
  fanart: https://artworks.thetvdb.com/banners/fanart/original/71187-3_t.jpg
- airdate: '2022-07-22T02:00:00Z'
  release: $day, $time
  flag: false
  title: The Old Man
  episode: VII
  number: S01E07
  runtime: 64
  studio: Hulu
  rating: ''
  genres: Action, Drama, Thriller
  poster: >-
    https://artworks.thetvdb.com/banners/v4/series/367392/posters/629cef6e884e5_t.jpg
  fanart: >-
    https://artworks.thetvdb.com/banners/v4/series/367392/backgrounds/6297a730edc11_t.jpg
- airdate: '2022-07-22T04:00:00Z'
  release: $day, $time
  flag: false
  title: For All Mankind
  episode: Bring It Down
  number: S03E07
  runtime: 60
  studio: Apple TV+
  rating: ''
  genres: Drama, Science Fiction
  poster: >-
    https://artworks.thetvdb.com/banners/series/356202/posters/600304db98362_t.jpg
  fanart: >-
    https://artworks.thetvdb.com/banners/series/356202/backgrounds/6035368ae4176_t.jpg
- airdate: '2022-07-25T01:00:00Z'
  release: $day, $time
  flag: false
  title: Westworld
  episode: Zhuangzi
  number: S04E05
  runtime: 58
  studio: HBO
  rating: ★ 8.9
  genres: Adventure, Drama, Mystery, Science Fiction, Western
  poster: https://artworks.thetvdb.com/banners/posters/296762-3_t.jpg
  fanart: https://artworks.thetvdb.com/banners/fanart/original/296762-8_t.jpg
- airdate: '2022-07-26T00:00:00Z'
  release: $day, $time
  flag: false
  title: The Bachelorette
  episode: Episode 3
  number: S19E03
  runtime: 85
  studio: ABC (US)
  rating: ★ 6.3
  genres: Drama, Game Show, Reality, Romance
  poster: https://artworks.thetvdb.com/banners/posters/71187-8_t.jpg
  fanart: https://artworks.thetvdb.com/banners/fanart/original/71187-3_t.jpg

friendly_name: Sonarr_Upcoming_Media