If state contains string?

How can I get a template to check for a specific set of text within a state’s string? For example, I’d like to have X happen if the sensor.dark_sky_minutely_summary contains the string “cloud” (e.g. it contains “partly cloudy” or “clouds”). I’m looking to set something up where if the sun is up, and the forecast says “cloud” in the string, then the lights come on:
- alias: ‘Cloudy Day’
trigger:
- platform: state
entity_id: sun.sun
state: ‘above_horizon’
condition:
condition: and
conditions:
- condition: template
value_template: ‘{{ states.sensor.dark_sky_minutely_summary.state CONTAINS??? “cloud” }}’
- condition: or
conditions:
- condition: state
entity_id: ‘device_tracker.user1’
state: ‘home’
- condition: state
entity_id: ‘device_tracker.user2’
state: ‘home’
action:
- service: script.lights_office_energizing

1 Like

Google something like “jinja string contains”

This showed up:

{% if 'index.html' in request.build_absolute_uri %}
    hello
{% else %}
    bye
{% endif %}

See if that works for you, this may not be correct but at least a guideline of what to search for.

edit - tested it, should work fine

6 Likes

Side note to maybe better understand why this works. Jinja is python in python every string is a array of characters…also if pasting code @photoronin please use preformatted text :slight_smile:

~Cheers

1 Like

I didn’t actually know that… well that’s useful to know :slight_smile:

Thanks, folks! that was just what I was looking for, and clears up a number of other questions as well.

Hi @photoronin, I’m trying to do the same thing (check for a specific set of text in state entity, for ex. “loud”), could you share how you solved?
From the example shown in the answers I cannot understand.

this should do it if you substitute your entity_id

{% if 'loud' in states('your_entity_id') %}
    hello
{% else %}
    bye
{% endif %}
3 Likes

Trying something like this:

  - platform: template
    sensors:
      sensor_number:
        friendly_name: Sensor Number
        value_template: >
          {% if '1' in states('sensor.serial_sensor.split('+')[0] ') -%}                
            hello
          {% else -%}
            bye
          {% endif -%}

But it only gives me ‘bye’.

The output for sensor.serial_sensor is this: 1+1812.50+22.9+1

Since only the first character of that string resembles the sensor number, I’m trying to find a way to identify that number separately from the string. Any thoughts?

maybe I’m wrong but this does not look like jinja…
I mean you split before you get the state

states('sensor.serial_sensor').split('+')[0]

^^ at least gets the state then splits. But using dots feels more like a object oriented programming… Jinja uses | .

I would guess it’s supposed to be

states('sensor.serial_sensor') | split('+')[0]

But since it’s the first character you want then you can just use substring

states('sensor.serial_sensor')[0:1]

2 Likes

You rock! Thanks for helping out!

“split” is a Python method. So it uses the “dot” notation.

filters use the | notation.

So your first example is correct but the second one isn’t.

And, as you rightly pointed out, @Potterstraat’s definitely wasn’t. :wink:

Hi,

I have a automation for when we watch something on Netflix, for example, the cinema mode activates. But I want when the title of the content contains the word ‘episode’ the action don’t start. So I want only the cinema mode when we watch a movie and not with a serie.

Hope you guys can help me out!

Thx!

It’s a lot easier to help if you post the automation

This is the automation I have. I want only to dim the lights when a movie is played and not a serie:

alias: Turn on bios mode when watch Disney+ on Sony tv
description: “”
trigger:

  • platform: device
    device_id: a35926ed4cf69a3b0d6328a30f9c619e
    domain: media_player
    entity_id: media_player.sony_tv
    type: playing
    for:
    hours: 0
    minutes: 1
    seconds: 0
    condition:
  • condition: state
    entity_id: media_player.sony_tv
    attribute: app_name
    state: Disney+
  • condition: device
    type: is_on
    device_id: 4d6e1fcb44f7957c784bf2c965bdc198
    entity_id: light.lamp_tv
    domain: light
  • condition: numeric_state
    entity_id: sensor.lux_sensor_illuminance_lux
    below: “15”
    action:
  • service: scene.turn_on
    data:
    transition: 20
    target:
    entity_id: scene.bios_game_mode
    mode: single

Remember to post code inside code blocks or the formatting is wrong and we can’t use the code.
But this should be a condition in your automation

  - condition: template
    value_template: "{{ ('episode' in state_attr('media_player.sony_tv', 'title')) == false }}"
2 Likes