Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set volume for notifications #1792

Open
tbarbette opened this issue Dec 6, 2022 · 26 comments
Open

Set volume for notifications #1792

tbarbette opened this issue Dec 6, 2022 · 26 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@tbarbette
Copy link

tbarbette commented Dec 6, 2022

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...

@alandtse alandtse added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Dec 10, 2022
@LunkwillAndFook
Copy link

@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?

@tbarbette
Copy link
Author

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

@klatka
Copy link

klatka commented Feb 17, 2023

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: []

@tbarbette
Copy link
Author

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 :)

@klatka
Copy link

klatka commented Feb 17, 2023

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

@github-actions
Copy link

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

@tbarbette
Copy link
Author

Nooo :) We want that feature ! :p

@github-actions
Copy link

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

@tbarbette
Copy link
Author

Nope, still want the feature ! :)

@github-actions
Copy link

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

@tbarbette
Copy link
Author

Still not solved and no easy way to solve :)

@iaah05
Copy link

iaah05 commented Oct 4, 2023

This would be super useful!

@danielbrunt57
Copy link

danielbrunt57 commented Nov 7, 2023

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

@jasonalsing
Copy link

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

@dade80vr
Copy link

Up! We still need it!

@danielbrunt57
Copy link

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
Copy link

Up! We still need it!

Then propose a workable solution!

@danielbrunt57
Copy link

All of my echo volume_level attributes are set...

image

@hiagocosta
Copy link

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

Copy link

github-actions bot commented Apr 7, 2024

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

@joamla96
Copy link

joamla96 commented Apr 7, 2024

Updoot

@alfgomes
Copy link

alfgomes commented Apr 8, 2024

That's Possible?

@danielbrunt57
Copy link

That's Possible?

What?

@BBE-FR
Copy link

BBE-FR commented Apr 17, 2024

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...

@tbarbette
Copy link
Author

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.

@BBE-FR
Copy link

BBE-FR commented Apr 17, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests