Search for string within Sensor Attributes Automation

Hi All,

I am trying to figure out how to search a sensor’s [nws_alerts] attribute [title] for ‘Tornado Warning.’ If “Tornado Warning” is found anywhere inside, it turns on switch “Tornado Warn HA.” Below is what I came up with, but I am hoping someone can check it out. For two reasons:

1.) I read somewhere else on the forum that at least at one point, “state” platforms couldn’t use “templates.”

2.) I don’t understand what “in [trigger.to_state.state]” means… i.e. if that should say something else.

Appreciate it!

Dallas

- id: '1907862912346'
  alias: Handle Tornado Alert Count to Hubitat
  description: ''
  trigger:
  - platform: state
    entity_id:
    - sensor.nws_alerts
  condition: []
  action:
  - if:
    - condition: state
      entity_id: sensor.nws_alerts
      attribute: title
      value_template: "{{'Tornado Warning' in [trigger.to_state.state]}}"
    then:
    - type: turn_on
      device_id: 78ea3f531f6af74090d2c284f899822c
      entity_id: tornado_warn_ha
      domain: switch
    else:
    - type: turn_off
      device_id: 78ea3f531f6af74090d2c284f899822c
      entity_id: tornado_warn_ha
      domain: switch
  mode: single

The sate condition does not have a value_template option. See https://www.home-assistant.io/docs/scripts/conditions/#state-condition. You could use a template condition instead.

This will return the “to” state value of whatever entity caused the automation to trigger.

As you only have one trigger it will return the value sensor.nws_alerts changed to. You can see a full list of available variables for each trigger type here: Automation trigger variables - Home Assistant

Try this:

  action:
  - if:
    - condition: template
      value_template: "{{'Tornado Warning' in state_attr(trigger.entity_id,'title') }}"

As you only have one trigger you could also just do:

  action:
  - if:
    - condition: template
      value_template: "{{'Tornado Warning' in state_attr('sensor.nws_alerts','title') }}"

Appreciate it!