Alexa tts volume

Hi there, I want alexa to speak when I switch my lights on or off.
This is what I got so far

- id: '1546876295650'
  alias: kitchenLightsOn
  trigger:
  - entity_id: switch.keuken
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - data:
      entity_id: media_player.bas_echo_dot
      volume_level: '.5'
    service: media_player.volume_set
  - data:
      entity_id: media_player.bas_echo_dot
      message: Kitchen lights on
    service: media_player.alexa_tts
- id: '1546876427739'
  alias: KitchenLightsOff
  trigger:
  - entity_id: switch.keuken
    from: 'on'
    platform: state
    to: 'off'
  condition: []
  action:
  - data:
      entity_id: media_player.bas_echo_dot
      volume_level: '.5'
    service: media_player.volume_set
  - data:
      entity_id: media_player.bas_echo_dot
      message: Kitchen lights off
    service: media_player.alexa_tts

The volume changed correctly to .5 but is there a way to check the original volume?
If I have the echo dot on volume 10 and toggle the lights it stays at 5.

thankyou

Well, first you can combine this into a single automation using templates. Second, you can get the current volume using templates. To get the current volume and set it back, you need to store the information. This can be a pain in the ass. To get around that, we can simply create a script that handles this, then pass a variable to it. That variable can then be used anywhere in the script. We can make this script handle any light as well, also using variables.

automation

- alias: kitchenLightsOn
  trigger:
  - entity_id: switch.keuken
    platform: state
  condition:
    condition: template
    value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
  action:
    service: script.announce_light_change
    data_template:
      alexa_device: media_player.bas_echo_dot
      alexa_volume: "{{ state_attr('media_player.bas_echo_dot', 'volume') }}"
      alexa_message: >
        {{ state_attr(trigger.entity_id, 'friendly_name') }} {{ trigger.to_state.state }}

script

  announce_light_change:
    sequence:
      - service: media_player.volume_set
        data_template:
          entity_id: "{{ alexa_device }}"
          volume_level: 0.5
      - service: service: media_player.alexa_tts
        data_template:
          entity_id: "{{ alexa_device }}"
          message: "{{ alexam_message }}"
      - service: media_player.volume_set
        data_template:
          entity_id: "{{ alexa_device }}"
          volume_level: "{{ alexa_volume }}"

So what this does is it passes 3 variables. Those variables are alexa_device, alexa_volume, and alexa_message.

When you add them to the data, or data_template section for the script service, the values in the data section are passed via those variables. So if I make this data section with the following script service:

    service: script.announce_light_change
    data_template:
      alexa_device: media_player.my_alexa_device
      alexa_volume: 1.0
      alexa_message: "SUPER LOUD"

The script will recieve 3 variables, alexa_device (which contains media_player.my_alexa_device), alexa_volume (containing 1.0), and alexa_message (contianing SUPER LOUD).

The script accesses the variables through what is called ‘templating’.

It is an advanced topic. Anyways, the script accesses the variables through templating in each data_template section.

7 Likes

Thanks for explaining in detail! What i got so far is

Scripts

announce_light_change:
  sequence:
  - service: media_player.volume_set
    data_template:
      entity_id: '{{ alexa_device }}'
      volume_level: 0.5
  - service: media_player.alexa_tts
    data_template:
      entity_id: '{{ alexa_device }}'
      message: '{{ alexa_message }}'
  - service: media_player.volume_set
    data_template:
      entity_id: '{{ alexa_device }}'
      volume_level: '{{ alexa_volume }}'    

Automation

- id: '1554983420926'
  alias: kitchenLightsOn
  trigger:
  - entity_id: switch.keuken
    platform: state
  condition:
    condition: template
    value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
  action:
    service: script.announce_light_change
    data_template:
      alexa_device: media_player.bas_echo_dot_2
      alexa_volume: "{{ state_attr('media_player.bas_echo_dot_2', 'volume') }}"
      alexa_message: >
        {{ state_attr(trigger.entity_id, 'friendly_name') }} {{ trigger.to_state.state }}

When I turn on my switch I get this error message:

Error while executing automation automation.kitchenlightson. Invalid data for call_service at pos 1: expected float for dictionary value @ data['volume_level']

Does this actually retrieve the current volume and set it back or are you hard coding a volume level to set things to when done with the announcement?

It sends the current volume to a script as variable “alexa_volume”. The script then sets the TTS volume to 0.5 and plays the message. Lastly, it sets the volume level to “alexa_volume”

Thanks Petro. I posted this in the other Alexa thread but will probably get lost as it’s so big. Does HA support an announcement volume as opposed to the main volume? When I was using OpenHAB there was a separate volume level you could manage just for alerts and the like which was separate from the main volume used for things like music.

I believe it does. You’d have to verify with the component though. This thread is about a custom component alexa_media_player.

Yep I have it implemented and that’s what I’m using. Also posted this question in the alexa_media_player main post but it’s huge.

doesn’t look like it supports a volume level for announcements. However you could build that in pretty easily. Either with an input number or the custom variable component. Use the script above to make it happen.

What if I have the media_player.volume_set service but not the media_player.alexa_tts service? I do have the notify.alexa_media service. Not sure if we are supposed to change things to that? Also not sure if that changes how the parameters are passed within the script or not.

You have to add it in your notify section. The script will change because the service listed in the script is deprecated. The new version is notify.alexa_media and you populate the fields based on what you want to do. My repository has examples in the scripts.yaml.

Thanks yeah figured it out and was just coming here to post that. That said, do you find it works all the time? I’m doing an announce vs a TTS because I want the noise to play before it talks, however when I trigger it manually thru the GUI it doesn’t do anything. Then the second time it does. Nothing in the logs but it does this everytime. Didn’t do anything that auto triggers but thinking shouldn’t matter.

By the way so I can understand, what part of the script actually saves the original volume level unless I didn’t understand the original ops ask. Looks like it

  1. Sets the volume on the Alexa to 50%
  2. says something
  3. Sets the volume to I guess whatever you pass to it?

So this doesn’t seem to retrieve and store the current setting, and then apply it at the end again.

is the assumption above that they always want the volume to 50% when announcing something, and then we can pass to the script what we want it changed “back” to when it’s done?

It does, when you call the script via this service in the automation:

    service: script.announce_light_change
    data_template:
      alexa_device: media_player.bas_echo_dot_2
      alexa_volume: "{{ state_attr('media_player.bas_echo_dot_2', 'volume') }}"
      alexa_message: >
        {{ state_attr(trigger.entity_id, 'friendly_name') }} {{ trigger.to_state.state }}

That service grabs the value prior to running the script and passes the value into the script. That value is used in the last service call.

Also, I never saw @basziee’s response because he didn’t reply to me, only to the thread which doesn’t notify. So to fix his issue, the last service in the script should read:

  - service: media_player.volume_set
    data_template:
      entity_id: '{{ alexa_device }}'
      volume_level: '{{ alexa_volume | float }}'  

I haven’t used the announce service, but I would assume it’s due to timing. The tts service works every time if I use it. It only gets suppressed when you call it twice really fast. Don’t know which one is getting suppressed.

Thanks petro have to try TTS instead and see how that responds. So should this be returning None or am I entering it wrong?

I do not see volume as an attribute.

i believe the attribute is volume_level

nevermind, you should have a volume and you have an odd supported features number. What device is that

It’s an Echo Plus 2nd Gen.

By the way, my 4th gen eccobee with Alexa voice returns the same result and also has the same supported features number.

I was remembering wrong about the supported features. As for the volume, you should have it. You may need to put the device in idle or playing in order to see the values then.

Not sure I did it right but went to STATES and pulled up my Alexa device, and changed States to idle and it stuck for a coupe minutes but still returned none on the volume.