Trigger when last notification contains a specific phrase

Trying to get an automation to turn on a boolean when it detects that a certain phrase has been sent as a notification to my phone. I’ve looked around and there’s value templates and regex statements but having trouble understanding/adapting it to suit my needs. Here’s what I have so far:

alias: Order Confirmed
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.phone_last_notification
    to: has been confirmed
condition: []
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.order_confirmed
mode: single

Which of course only works if the exact phrase ‘has been confirmed’ is the whole notification. But there’s some variables before and after that I want to ignore.

Help is much appreciated, thanks!

Try this in a template trigger:

{{ 'has been confirmed' in states('sensor.phone_last_notification') }}
1 Like

That worked beautifully and way more simple than I thought it would be. Thanks!

1 Like

I’m really new to all this. Can you please elaborate on how this would be incorporated into the OP’s code?

alias: Order Confirmed
description: ""
trigger:
  - platform: template
    value_template: "{{ 'has been confirmed' in states('sensor.phone_last_notification') }}"
condition: []
action:
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.order_confirmed
mode: single
trigger:
  - platform: state
    entity_id:
      - sensor.phone_last_notification
    to: has been confirmed

Would become:

trigger:
  - platform: template
    value_template: "{{ 'has been confirmed' in states('sensor.phone_last_notification') }}"

I beat you to it. :wink:

1 Like

Thanks guys!

Does the action need to be a boolean, or can it just be a regular device action (eg turn off lights)? I’m asking because I tried what you suggested, but with a device action, and it’s not working.

I saw lol.
I had to go and Google what you use for template - I couldn’t remember if it was value, or state or something else (which of course it is).

1 Like

It can be literally any service that you can call.
Be aware though - in the examples me and @finity have posted - the case of the phrase that is being looked for in the notification is important.

has been Confirmed is not the same as has been confirmed
The phrase you are looking for, must have the correct casing (or you must use a filter to convert it)

    value_template: "{{ 'has been confirmed' in states('sensor.phone_last_notification')|lower }}"

EDIT:
I would also point out - that it probably won’t trigger if the last notification received, is the same text as the previous state of the sensor. Though someone would need to confirm that.

I don’t know what that means. Specifically what isn’t working? is it not triggering or is it only just not running the actions?

Here’s what I have (I’ve replaced the device ID with xyz, in case that is sensitive info):

alias: Front door unlocked
description: ""
trigger:
  - platform: template
    value_template: "{{ 'unlocked' in states('sensor.josh_note_20_ultra_last_notification') }}"
condition: []
action:
  - type: turn_off
    device_id: xyz
    entity_id: light.basement_lights
    domain: light
mode: single

When I unlock the front door, my phone receives the notification with the word" unlocked" in it, but the lights don’t turn off. I’ve confirmed that the Last Notification in the companion app is in fact picking up the notification.

Can you post a screenshot of the mobile app notification sensor showing the state when it gets that notification?

You mean this?

Am I right in thinking that if the state of the sensor is already the notification that says unlocked in it, a new notification with the exact same text in it, won’t change the state of the sensor - and thus the trigger won’t fire?

Nevermind I see that the notification has the time in it - and thus the state should update.

Can we see the sensor in the developer tools - states page in Home Assistant itself ?

This?

1 Like

Yup - now go in the automation and click on “Traces” and then click on the 3 dots in the top right and then “Download Trace” - and paste it in to a post on here.
Make sure you use the code tag so it displays properly in the post.

{
  "trace": {
    "last_step": "action/0",
    "run_id": "2e1e39715d3b7ebf0f11ef186c245266",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2022-10-02T00:13:52.017576+00:00",
      "finish": "2022-10-02T00:13:52.034382+00:00"
    },
    "domain": "automation",
    "item_id": "1664666077564",
    "trigger": "sensor.josh_note_20_ultra_last_notification via template",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2022-10-02T00:13:52.017658+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.front_door_locked",
              "state": "on",
              "attributes": {
                "last_triggered": "2022-10-01T23:54:02.848997+00:00",
                "mode": "single",
                "current": 0,
                "id": "1664666077564",
                "friendly_name": "Front door unlocked"
              },
              "last_changed": "2022-10-02T00:07:51.240222+00:00",
              "last_updated": "2022-10-02T00:07:51.240222+00:00",
              "context": {
                "id": "01GEB0AH688YFHE9562YM8EGYY",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "platform": "template",
              "entity_id": "sensor.josh_note_20_ultra_last_notification",
              "from_state": {
                "entity_id": "sensor.josh_note_20_ultra_last_notification",
                "state": "Levinsin Home: The Front door bell camera detected motion.",
                "attributes": {
                  "android.appInfo": "ApplicationInfo{18d7e6f com.alarm.alarmmobile.android.telus}",
                  "android.bigText": "Levinsin Home: The Front door bell camera detected motion.",
                  "android.infoText": "null",
                  "android.largeIcon": "null",
                  "android.progress": 0,
                  "android.progressIndeterminate": false,
                  "android.progressMax": 0,
                  "android.reduced.images": true,
                  "android.remoteInputHistory": "null",
                  "android.showChronometer": false,
                  "android.showWhen": true,
                  "android.subText": "null",
                  "android.template": "android.app.Notification$BigTextStyle",
                  "android.text": "Levinsin Home: The Front door bell camera detected motion.",
                  "android.title": "Front door bell Detected Motion",
                  "android.title.big": "Front door bell Detected Motion",
                  "android.wearable.EXTENSIONS": "{background=android.graphics.Bitmap@e9ed77c}",
                  "androidx.core.app.extra.COMPAT_TEMPLATE": "androidx.core.app.NotificationCompat$BigTextStyle",
                  "category": "null",
                  "channel_id": "NORMAL_NOTIFICATION_CHANNEL_ID",
                  "group_id": "7132102",
                  "is_clearable": true,
                  "is_ongoing": false,
                  "package": "com.alarm.alarmmobile.android.telus",
                  "post_time": 1664669615010,
                  "icon": "mdi:bell-ring",
                  "friendly_name": "Josh Note 20 Ultra Last Notification"
                },
                "last_changed": "2022-10-02T00:13:35.593747+00:00",
                "last_updated": "2022-10-02T00:13:35.593747+00:00",
                "context": {
                  "id": "01GEB0N1F9SCZGG7TF2ERPWKDK",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "sensor.josh_note_20_ultra_last_notification",
                "state": "Levinsin Home: The Front Door was unlocked at 5:13 PM on 2022-10-01.",
                "attributes": {
                  "android.appInfo": "ApplicationInfo{10f9d57 com.alarm.alarmmobile.android.telus}",
                  "android.bigText": "Levinsin Home: The Front Door was unlocked at 5:13 PM on 2022-10-01.",
                  "android.infoText": "null",
                  "android.largeIcon": "null",
                  "android.progress": 0,
                  "android.progressIndeterminate": false,
                  "android.progressMax": 0,
                  "android.reduced.images": true,
                  "android.remoteInputHistory": "null",
                  "android.showChronometer": false,
                  "android.showWhen": true,
                  "android.subText": "null",
                  "android.template": "android.app.Notification$BigTextStyle",
                  "android.text": "Levinsin Home: The Front Door was unlocked at 5:13 PM on 2022-10-01.",
                  "android.title": "Front Door Unlocked (Manually unlocked)",
                  "android.title.big": "Front Door Unlocked (Manually unlocked)",
                  "android.wearable.EXTENSIONS": "{background=android.graphics.Bitmap@de0b044}",
                  "androidx.core.app.extra.COMPAT_TEMPLATE": "androidx.core.app.NotificationCompat$BigTextStyle",
                  "category": "null",
                  "channel_id": "NORMAL_NOTIFICATION_CHANNEL_ID",
                  "group_id": "7132102",
                  "is_clearable": true,
                  "is_ongoing": false,
                  "package": "com.alarm.alarmmobile.android.telus",
                  "post_time": 1664669631478,
                  "icon": "mdi:bell-ring",
                  "friendly_name": "Josh Note 20 Ultra Last Notification"
                },
                "last_changed": "2022-10-02T00:13:52.016140+00:00",
                "last_updated": "2022-10-02T00:13:52.016140+00:00",
                "context": {
                  "id": "01GEB0NHGGNCPWY69EHZXC2DT5",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "id": "0",
              "idx": "0",
              "alias": null,
              "for": null,
              "description": "sensor.josh_note_20_ultra_last_notification via template"
            }
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2022-10-02T00:13:52.023348+00:00",
          "changed_variables": {
            "context": {
              "id": "01GEB0NHGHRHMTBJ19AB92YHJ7",
              "parent_id": "01GEB0NHGGNCPWY69EHZXC2DT5",
              "user_id": null
            }
          }
        }
      ]
    },
    "config": {
      "id": "1664666077564",
      "alias": "Front door unlocked",
      "description": "",
      "trigger": [
        {
          "platform": "template",
          "value_template": "{{ 'unlocked' in states('sensor.josh_note_20_ultra_last_notification') }}"
        }
      ],
      "condition": [],
      "action": [
        {
          "type": "turn_off",
          "device_id": "079aa358a9ba289df053a6585f90da95",
          "entity_id": "light.basement_lights",
          "domain": "light"
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01GEB0NHGHRHMTBJ19AB92YHJ7",
      "parent_id": "01GEB0NHGGNCPWY69EHZXC2DT5",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "Front door unlocked",
      "message": "triggered by sensor.josh_note_20_ultra_last_notification via template",
      "source": "sensor.josh_note_20_ultra_last_notification via template",
      "entity_id": "automation.front_door_locked",
      "context_id": "01GEB0NHGHRHMTBJ19AB92YHJ7",
      "when": 1664669632.018001,
      "domain": "automation"
    },
    {
      "when": 1664669632.426992,
      "state": "off",
      "entity_id": "light.basement_lights",
      "context_event_type": "automation_triggered",
      "context_domain": "automation",
      "context_name": "Front door unlocked",
      "context_message": "triggered by sensor.josh_note_20_ultra_last_notification via template",
      "context_source": "sensor.josh_note_20_ultra_last_notification via template",
      "context_entity_id": "automation.front_door_locked"
    }
  ]
}

I just noticed something really weird. The automation actually seems to work when the door LOCKS (and it gives the corresponding notification that says it locked).