Substring search of a state aka using regex_search()

Hello,

I’d like to search the state of states.sensor.dark_sky_minutely_summary.state for a substring *.rain*.. I believe I can use the regex_search jinja function, but I’m not sure as this is the first time I’m attempting such a thing.

For example, here is the template trigger I’m thinking may work:

- id: '1537647311226'
  alias: Rain in the next hour
  trigger:
  - platform: template
    value_template: "{% if regex_search(states.sensor.dark_sky_minutely_summary.state, '*.rain*.', ignorecase=TRUE) %} {% endif %}"
  condition:
  - after: '10:00'
    before: '23:00'
    condition: time
  action:
  - data:
      entity_id: media_player.mpd
      message: Initiating weather report.  DarkSky reports rain coming in the next
        hour.  Thank you.
    service: tts.voicerss_say

Will this work? If so, how can I test this (for instance by forcing a state change for sensor.dark_sky_minutely_summary.state).

Thanks!

Wow, talk about a coincidence! I just had to figure this out in the last few days myself and mine was also weather related too. And it wasn’t very easy to find. But it was pretty funny that it was exceedingly easy to do once I figured out the syntax.

I needed to search a string in a sensor attribute for the word “warning” and set a logic statement as “true” or “false” based on if I found it or not.

Here is what I ended up doing:

- condition: template
  value_template: >
    {{ 'Warning' in states.sensor.pws_alerts.attributes.Description }}

that’s it.

What it does is retrieves the string from the “description” attribute of the sensor and returns ‘true’ if the word ‘warning’ is found in the string or ‘false’ if it doesn’t. And it’s not case sensitive. I’m sure there’s some kind of flag to make it case sensitive but I didn’t need (or want) it to be.

As far as testing it just create a template sensor with the proper state with and without the search string and then run an automation against it. Or easier still set up a template in the section of the dev-tools area and test if it returns true or false as desired.

My use case was for deciding whether my echo devices would make an emergency announcement in the middle of the night or not. I didn’t want to get woken up because it was foggy outside at 3 am.

Hope it helps.

3 Likes

Awesome! It’s times like this that the internet is a magical thing.

Note that in is listed on http://jinja.pocoo.org/docs/2.10/templates/#other-operators

I’m still unclear how I can test this. Note that I’m new to testing, etc. If I access states
(http://hassio.local:8123/dev-state) then change the state… state_changed must fire, and then the automation trigger is hit, if the state reflects in my care “rain” ?

[UPDATE]

Yes, the above works! If I change the state and include the string “rain” on the …/dev-state web page, the automation triggers. Sweet!

Thank you!

Here is the working automation:

- id: '1537647311226'
  alias: Rain in the next hour
  trigger:
  - platform: template
    value_template: "{{ 'rain' in states.sensor.dark_sky_minutely_summary.state }}"
  condition:
  - after: '10:00'
    before: '23:00'
    condition: time
  action:
  - data:
      entity_id: media_player.mpd
      message: 3. 2. 1. Initiating weather report.  DarkSky reports rain coming in the next
        hour.  Thank you.
    service: tts.voicerss_say
2 Likes

Glad I could help. That’s why we’re here. Hopefully it saved you from searching on google for an hour or more trying to find it like I did.

1 Like