alexa_media_player icon indicating copy to clipboard operation
alexa_media_player copied to clipboard

Set volume for notifications

Open tbarbette opened this issue 2 years ago • 28 comments

Is your feature request related to a problem? Please describe. Enable volume_level in short play_media notifications, like:

      - service: media_player.play_media
        target:
          entity_id: media_player.echo_bedroom
        data:
          media_content_id: amzn_sfx_doorbell_chime_01
          media_content_type: sound
          volume_level: 1
        metadata: {}

Currently, it doesn't work, volume_level is not supported.

Describe the solution you'd like One way to implement it would be to record the current volume, play the notification, then set back the volume. Consider I have 3 devices, doing it manually is complicated...

Describe alternatives you've considered I set the volume to maximum on the 3 devices, play my doorbell chime, then set it back to 0.5. But depending on the room it's too much or not enough...

tbarbette avatar Dec 06 '22 11:12 tbarbette

@tbarbette Not sure if this is helpful or not, but I created an AppDaemon script awhile back to do exactly this (as well as some other things). You may have to fiddle with the delay a bit for your environment to give the echo time to respond to the set_volume request. You can call it via an event as follows...

event: speech_manager.announce_phrase_to_target
event_data:
  target: media_player.kitchen_echo
  phrases:
    - There is someone at the door.
    - >-
      Nobody told me we were expecting guests, but someone is at the door.
    - Someone is at the door, should I send them away?

LunkwillAndFook avatar Dec 13 '22 04:12 LunkwillAndFook

Thanks but it's not what I'm looking for :)

tbarbette avatar Jan 04 '23 15:01 tbarbette

If I understood your issue right then I faced this problem too. I wrote a script (maybe not the best way) for that:

script:
  kla_alexa_play:
    alias: Alexa Play
    icon: mdi:play
    mode: queued
    max: 10
    description:
      Falls Alexa sprechen darf (bestimmte Zeiten etc.), liest die dafür
      definierte Gruppe die als Parameter übergebene Nachricht vor
    fields:
      message:
        description: Nachricht, welche Alexa vorlesen soll
        example: Was darf ich für dich tun?
      alexa:
        description:
          Alexa, welche sprechen soll (optional, 
          es spricht der zuletzt aktive Echo)
        example: echo_show8
      type:
        description: Alexa Ausgabetyp [TTS/command/sequence] (optional)
        example: tts
      volume_level:
        description:
          Lautstärke in der die Nachricht vorgelesen werden soll [0-30]
          (optional, ursprüngliche Lautstärke wird wieder zurückgesetzt)
        example: "1"
      force:
        description: Überspringe die Prüfungen, ob Alexa reden darf (optional)
        example: "true"
    sequence:
      - condition: or
        conditions:
          - condition: template
            value_template: >
              {{ force is defined and force }}
          - condition: and
            conditions:
              - condition: state
                entity_id: binary_sensor.kla_alexa_is_allowed_to_speak
                state: "on"
              - condition: state
                entity_id: binary_sensor.kla_anyone_home
                state: "on"
      - choose:
          - conditions:
              - condition: template
                value_template: >
                  {{ alexa is defined and alexa != ''
                    and states('media_player.' + alexa) not in ['None', 'unknown', 'unavailable']
                    and volume_level is defined
                    and (0 <= (volume_level | int(0)) <= 30) }}
            sequence:
              - service: input_number.set_value
                data_template:
                  value: >
                    {% set alexa_media = 'media_player.' + alexa %}
                    {% if (state_attr(alexa_media, 'volume_level') | float(0.33)) > 0 %}
                      {{ state_attr(alexa_media, 'volume_level') | float(0.33) }}
                    {% else %}
                      {{ ((volume_level | int(0)) / 30 | float(0.33)) | round(2) }}
                    {% endif %}
                entity_id: input_number.kla_alexa_volume_tmp
              - condition: template
                value_template: >
                  {{ (state_attr('media_player.' + alexa, 'volume_level') | float(0.33)) !=
                    ((volume_level | int(0)) / 30 | float(0.33)) | round(2) }}
              - service: media_player.volume_set
                data_template:
                  entity_id: media_player.{{ alexa }}
                  volume_level: >
                    {{ ((volume_level | int(0)) / 30 | float(0.03)) | round(2) }}
        default: []
      - choose:
          - conditions:
              - condition: template
                value_template: >
                  {{ type is not defined or (type is defined and type == "tts") }}
            sequence:
              - service: >
                  {% if alexa is defined and alexa != '' -%}
                    notify.alexa_media_{{ alexa }}
                  {%- elif states(states('sensor.kla_alexa_last_active')) not in ['None', 'unknown', 'unavailable'] -%}
                    notify.alexa_media_{{ states('sensor.kla_alexa_last_active').split('.')[1]}}
                  {% endif %}
                data_template:
                  data:
                    type: tts
                  message: "{{ message }}"
          - conditions:
              - condition: template
                value_template: >
                  {{ type is defined and type == "command" }}
            sequence:
              - service: media_player.play_media
                data_template:
                  entity_id: >
                    {% if alexa is defined and alexa != '' -%}
                      media_player.{{ alexa }}
                    {%- elif states(states('sensor.kla_alexa_last_active'))
                      not in ['None', 'unknown', 'unavailable'] -%}
                    {{ states('sensor.kla_alexa_last_active') }}{% endif %}
                  media_content_id: "{{ message }}"
                  media_content_type: custom
          - conditions:
              - condition: template
                value_template: >
                  {{ type is defined and type == "sequence" }}
            sequence:
              - service: media_player.play_media
                data_template:
                  entity_id: >
                    {% if alexa is defined and alexa != '' -%}
                      media_player.{{ alexa }}
                    {%- elif states(states('sensor.kla_alexa_last_active'))
                      not in ['None', 'unknown', 'unavailable'] -%}
                    {{ states('sensor.kla_alexa_last_active') }}{% endif %}
                  media_content_id: "{{ message }}"
                  media_content_type: sequence
        default: []
      - choose:
          - conditions:
              - condition: template
                value_template: >
                  {{ alexa is defined and alexa != ''
                    and states('media_player.' + alexa) not in ['None', 'unknown', 'unavailable']
                    and volume_level is defined
                    and (0 <= (volume_level | int(0)) <= 30)
                    and state_attr('media_player.' + alexa, 'volume_level') !=
                    states('input_number.kla_alexa_volume_tmp') }}
            sequence:
              - service: media_player.volume_set
                data_template:
                  entity_id: >
                    {{ 'media_player.' + alexa }}
                  volume_level: >
                    {{ states('input_number.kla_alexa_volume_tmp') | float(0.33) }}
        default: []

klatka avatar Feb 17 '23 13:02 klatka

The temporary input to remember the volume is a nice hack, but with multiple devices I have to make 3 of them, it would deserve a built-in feature :)

tbarbette avatar Feb 17 '23 14:02 tbarbette

I absolutely agree. I set the mode to queued to avoid this problem as a tradeoff only to play one device simultaneously.

klatka avatar Feb 17 '23 14:02 klatka

The issue has received no activity for 60 days and will be closed in a week.

github-actions[bot] avatar Apr 19 '23 02:04 github-actions[bot]

Nooo :) We want that feature ! :p

tbarbette avatar Apr 19 '23 08:04 tbarbette

The issue has received no activity for 60 days and will be closed in a week.

github-actions[bot] avatar Jun 19 '23 02:06 github-actions[bot]

Nope, still want the feature ! :)

tbarbette avatar Jun 19 '23 06:06 tbarbette

The issue has received no activity for 60 days and will be closed in a week.

github-actions[bot] avatar Aug 20 '23 01:08 github-actions[bot]

Still not solved and no easy way to solve :)

tbarbette avatar Aug 20 '23 08:08 tbarbette

This would be super useful!

iaah05 avatar Oct 04 '23 07:10 iaah05

Since volume_level is not supported, why not save the current volume levels automatically using snarky-snark/home-assistant-variables integration which I configure to update whenever Alexa Media Player: Update Last Called Sensor [service: alexa_media.update_last_called] is run which updates sensor.last_alexa.

Then simply set the new volume level in your script/automation, play the media and then set the volume back to the saved volume level from the var:

var:

  last_alexa:
    friendly_name: Last Alexa
    unique_id: last_alexa
    value_template: "{{ states('sensor.last_alexa') }}"
    tracked_entity_id: sensor.last_alexa
    attributes:
      media_player.garage_echo_dot: "{{ state_attr('media_player.garage_echo_dot','volume_level') }}"
      media_player.office_echo_dot_left: "{{ state_attr('media_player.office_echo_dot_left','volume_level') }}"
      media_player.office_echo_dot_right: "{{ state_attr('media_player.office_echo_dot_right','volume_level') }}"
      media_player.kitchen_echo_dot: "{{ state_attr('media_player.kitchen_echo_dot','volume_level') }}"
      media_player.living_room_echo_dot_left: "{{ state_attr('media_player.living_room_echo_dot_left','volume_level') }}"
      media_player.living_room_echo_dot_right: "{{ state_attr('media_player.living_room_echo_dot_right','volume_level') }}"
      media_player.bedroom_echo_dot_left: "{{ state_attr('media_player.bedroom_echo_dot_left','volume_level') }}"
      media_player.bedroom_echo_dot_right: "{{ state_attr('media_player.bedroom_echo_dot_right','volume_level') }}"
      media_player.daniel_s_echo: "{{ state_attr('media_player.daniel_s_echo','volume_level') }}"
      media_player.daniel_s_echo_auto: "{{ state_attr('media_player.daniel_s_echo_auto','volume_level') }}"
      media_player.daniel_s_echo_show_8_2nd_gen: "{{ state_attr('media_player.daniel_s_echo_show_8_2nd_gen','volume_level') }}"
    restore: true

Sequence to change volume & restore.to previous volume:

service: media_player.volume_set
data:
  volume_level: 0.5
target:
  entity_id: media_player.bedroom_echo_dot_left
service: media_player.play_media
target:
  entity_id: media_player.bedroom_echo_dot_left
data:
  media_content_id: amzn_sfx_doorbell_chime_01
  media_content_type: sound
metadata: {}
service: media_player.volume_set
data:
  volume_level: "{{ state_attr('var.last_alexa','media_player.bedroom_echo_dot_left') }}"
target:
  entity_id: media_player.bedroom_echo_dot_left

danielbrunt57 avatar Nov 07 '23 05:11 danielbrunt57

I have found often times volume_level is not set for a device so you can't retrieve it to save and restore.

jasonalsing avatar Nov 07 '23 19:11 jasonalsing

Up! We still need it!

dade80vr avatar Nov 21 '23 22:11 dade80vr

I have found often times volume_level is not set for a device so you can't retrieve it to save and restore.

If the volume_level is not even set then how do you expect the integration to know what to do??? That sounds like a totally different issue...

danielbrunt57 avatar Nov 22 '23 07:11 danielbrunt57

Up! We still need it!

Then propose a workable solution!

danielbrunt57 avatar Nov 22 '23 07:11 danielbrunt57

All of my echo volume_level attributes are set...

image

danielbrunt57 avatar Nov 22 '23 07:11 danielbrunt57

I absolutely agree. I set the mode to queued to avoid this problem as a tradeoff only to play one device simultaneously.

how did you do that? I see, that this is the only way for me to do this, because the volume level increases after door_chime_sound, even if the logic is used to do before it

hiagocosta avatar Feb 06 '24 18:02 hiagocosta

The issue has received no activity for 60 days and will be closed in a week.

github-actions[bot] avatar Apr 07 '24 02:04 github-actions[bot]

Updoot

joamla96 avatar Apr 07 '24 16:04 joamla96

That's Possible?

alfgomes avatar Apr 08 '24 21:04 alfgomes

That's Possible?

What?

danielbrunt57 avatar Apr 08 '24 23:04 danielbrunt57

There is another way arround this issue:

You can let the volume where it is and play with the message itself using ssml tag as described here: https://docs.aws.amazon.com/polly/latest/dg/supportedtags.html#prosody-tag

I personaly often use the whispering tag for notification at night, and the prosody to increase the volume of some notification above the normal speach volume.

This removes the need to memorize and reset the volume, but is not as effective if the volume as been set to extreme level...

BBE-FR avatar Apr 17 '24 13:04 BBE-FR

Thanks @BBESINET for this workaround. How do you concretely use the tags in HA though?

@danielbrunt57 this seems like a possible workaround indeed. I think having the ability to simply set the volume_level in play_media would be much simpler.

tbarbette avatar Apr 17 '24 14:04 tbarbette

an exemple for the whisper:

service: notify.alexa_media
data:
  message: >-
    <amazon:effect name="whispered">The washing machine is finished</amazon:effect>
  target:
    - media_player.echo_1
    - media_player.echo_2
  data:
    type: tts
  title: Washing machine cycle finished

another one for an Alexa doorbell using the prosody:

service: notify.alexa_media
data:
  title: "Doorbell"
  message: <prosody volume="loud">Somebody is at the front door</prosody> 
  data: 
    method: speak
    type: announce
  target: 
      - media_player.echo_1

as shown in the link of all supported ssml tags you can apply the tag to only a portion of the text if needed:

service: notify.alexa_media
data:
  message: >-
    <prosody volume="loud">Attention please </prosody> Somebody is at the front door
  target:
    - media_player.echo_1
  data:
    type: tts
  title: Test

BBE-FR avatar Apr 17 '24 15:04 BBE-FR

The issue has received no activity for 60 days and will be closed in a week.

github-actions[bot] avatar Jun 17 '24 02:06 github-actions[bot]

Up! We still need it!

alfgomes avatar Jun 17 '24 03:06 alfgomes

July 2024 and still haven't this feature :( It's too bad that a "simple" feature like that isn't avalable yet

ftt-prod avatar Jul 29 '24 19:07 ftt-prod

And tomorrow it'll be August! It seems so far to really only be me working hard on this integration and I'm new to the game. I'm still learning the ropes, plus I (like most people) have bills to pay and do not have endless free time.

Everyone thinks it's such a simple feature to implement. Stick your head inside the code and tell me how to do it...

danielbrunt57 avatar Jul 31 '24 23:07 danielbrunt57