How to make Sonos a notify entity?

Hello Community!

I have a set of Sonos speakers and I set them up to tell me (/w Google TTS) when doors and windows open and close, it’s really nice. I recently learned (tonight) how to use the alert component to tell me, for example, if the garage doors are open for too long after dark:

alert_garage_doors_at_night:
  name: Alert if garage doors are open after dark
  title: Garage doors alert 
  message: >
    {% for entity in states.group.all_garage_doors.attributes.entity_id if is_state(entity, 'open') %}
      {%- if loop.first and loop.last %}
        {{ states[entity.split('.')[0]][entity.split('.')[1]].name }} is open
      {% else %}
        {%- if loop.first %}{% elif loop.last %} & {% else %}, {% endif -%}
        {{ states[entity.split('.')[0]][entity.split('.')[1]].name }}{{ " are open" if loop.last }}
      {%- endif -%}
    {% endfor %}
  done_message: Garage doors were alerted but are now closed
  entity_id: binary_sensor.garage_doors_alert
  state: 'on'
  repeat: 15
  skip_first: true
  can_acknowledge: true
  notifiers:
    - vapid
  data:
    tag: garage_doors

However, the alert takes a notify component (in notifiers) to send the message. I’d really like to use the TTS with Sonos to have the notification broadcast over the speakers but I can’t figure out how to make the Sonos TTS entities “notify component”-compatible. Is there a way?

Thanks,
-Greg

bugger I can read that and under stand what you are doing. Like it

thanks for posting bro

Create script
Brings the sonos back in same state as before

sonos_say:
  alias: "Sonos TTS script"
  sequence:
   - service: sonos.snapshot
     data_template:
       entity_id: "{{ sonos_entity }}"
   - service: sonos.unjoin
     data_template:
       entity_id: "{{ sonos_entity }}"
   - service: media_player.volume_set
     data_template:
       entity_id: "{{ sonos_entity }}"
       volume_level: "{{ volume }}"
#   - service: tts.google_translate_say
   - service: tts.voicerss_say
     data_template:
       entity_id: "{{ sonos_entity }}"
       message: "{{ message }}"
   - delay: "{{ delay }}"
   - service: sonos.restore
     data_template:
       entity_id: "{{ sonos_entity }}"

Call script with automation like this

- alias: 'test'
  trigger:
    - platform: state
      entity_id: input_boolean.mytest
  action:
    - service: script.sonos_say
      data:
        sonos_entity: media_player.sonosname
        volume: 0.5
        message: 'Test Message'
        delay: '00:00:05'
1 Like

Hi @memnom, I think you’ve missed the point of my original post. I already have the Sonos speakers working with automations. I’m trying to get it working specifically with an alert component.

I think @memnom gave the solution, you need to call the TTS Script with your Alert Message.

 action:
    - service: script.sonos_say
      data:
        sonos_entity: media_player.sonosname
        volume: 0.5
        message: >
              {% for entity in states.group.all_garage_doors.attributes.entity_id if is_state(entity, 'open') %}
      {%- if loop.first and loop.last %}
        {{ states[entity.split('.')[0]][entity.split('.')[1]].name }} is open
      {% else %}
        {%- if loop.first %}{% elif loop.last %} & {% else %}, {% endif -%}
        {{ states[entity.split('.')[0]][entity.split('.')[1]].name }}{{ " are open" if loop.last }}
      {%- endif -%}
    {% endfor %}

I must be missing something here because I’m using the alert component for this. There doesn’t seem to be any “action” section for it, only a “notifiers” section and the Sonos isn’t a notifier. I want the Sonos speakers to be triggered on the same 15 minute interval (except immediately [skip_first: true]).

@memnom’s config uses input_boolean.mytest to trigger the Sonos. It seems like you guys are both saying to just “use an automation instead of the alert component”. If I do that, how do I model the alert’s method of notifying every 15 minutes except for the initial trigger?

Ideally, I wouldn’t have to recreate all the logic and the message in a completely independent automation.

My bad, missed that.

Yes this is what I meant.

Sorry I couldn’t be of much help here, but would be really interested to know if you find a way around this.

FWIW, this template:

  message: >
    {% for entity in states.group.all_garage_doors.attributes.entity_id if is_state(entity, 'open') %}
      {%- if loop.first and loop.last %}
        {{ states[entity.split('.')[0]][entity.split('.')[1]].name }} is open
      {% else %}
        {%- if loop.first %}{% elif loop.last %} & {% else %}, {% endif -%}
        {{ states[entity.split('.')[0]][entity.split('.')[1]].name }}{{ " are open" if loop.last }}
      {%- endif -%}
    {% endfor %}

can be reduced to this:

  message: >
    {% set open_doors = expand('group.all_garage_doors') | selectattr('state','eq','open') | map(attribute='name') | list %}
    {{ open_doors | join(', ') }} {{ 'are' if open_doors | count > 1 else 'is' }} open
2 Likes