[solved] : Alert notifier to invoke a script

I have a script called “voice_notify” which I am calling for sending a voice alert on my google assistant. My script checks multiple criteria before sending the voice alerts to google home like “is the emergency mode is on”, “is guest mode on”, “is it bed time”, etc.

Now I started using alert component for my front door, garage door, etc. Is there a way I can create a notification component from this script so I can use it in my alert component. i tried it through group but it is still not working. Any one in this forum can shed some light?

Here is my partial voice_notify script,

script:
  voice_notify:
    sequence:
      - condition: or
        conditions:
          - condition: state
            entity_id: input_boolean.emergency_mode
            state: 'on'      
          - condition: state
            entity_id: input_boolean.do_not_disturb
            state: 'off'
      - condition: state
        entity_id: input_boolean.guest_mode
        state: 'off'
      - condition: state
        entity_id: input_boolean.voice_notifications
        state: 'on'      

Here is my alert,

alert:
  greenhouse_temp:
    name: Greenhouse temp is out of threshold
    entity_id: binary_sensor.gh_temp_alert
    repeat: 
      - 10
    notifiers:
      - group_voice_notify
      - telegram_notify
1 Like

There’s no direct way to do this but there is a tricky workaround. Take a look at this post:

The use case its describing is out of date since you no longer need to do this to create persistent notifications from alerts. But you can make any service call this way, just replace persistent_notification/create with script/your_script_name at the end of the URL

Thank you Mike, you suggestion worked. I am showing the code that worked for me for others benefit.

notify:
  - name: voice_google_notify
    platform: rest
    resource: 'http://localhost:8123/api/services/script/voice_notify'
    method: POST_JSON
    headers:
      authorization: !secret restapi_notify #create a long-term token in your profile page and add 'Bearer ' before it in your secrets file
      content-type: 'application/json'
    message_param_name: message
6 Likes

Thanks guys. although this is a very old post, it solved exactly my problem (call a script in alerts).

I simply wanted to pass all notification arguments, including additional target specific data. The latter one took some time to figure out it. here is an example how to pass all:


notify:
  - name: my_rest_notifier
    platform: rest
    resource: 'http://localhost:8123/api/services/script/my_notify_script'
    method: POST_JSON
    headers:
      authorization: !secret restapi_notify #create a long-term token in your profile page and add 'Bearer ' before it in your secrets file
      content-type: 'application/json'
    message_param_name: message
    title_param_name: title
    target_param_name: target
    data:
      data: '{{ data if data else {} }}'

This allows to pass for example this data to the rest notifier:

service: notify.my_rest_notifier
data:
  message: The garage door has been open for 10 minutes.
  title: Your Garage Door Friend
  target: mytarget
  data:
    icon: 81

All data provided will be passed to the notification script. This is how I call a notification service inside the script, dealing with data that is provided optionally:

      - service: notify.eric
        data:
          title: "{{ title | default('') }}"
          message: "{{ message }}"
          target: "{{ target | default([]) }}"
          data: "{{ data | default({}) }}"

Have fun

3 Likes

Nearly 4 years and this thread still helps! Thanks erkr, I implement your solution to be used with alerts

1 Like