Google home - set volume up and back again

My guess would be it has something to do with the delays. After sending the TTS command, it takes some time to process (generate the audio file, send the command to the speaker, download & play the audio file). If you send a new command in the meantime, like setting the volume, it might interfere.

So I would add some bigger delays after your tts.google_say commands.

You should be posting the error you’re getting in the log to help people diagnose your issue. In this case the problem is easy to spot. You’re missing some quotes. Volume level parameter also requires a float value.

volume_level: "{{ states(input_number.current_volume) }}"

Should be.

volume_level: "{{ states('input_number.current_volume')|float }}"

Thanks, @jazzyisj & @Emphyrio. Indeed it was a combination of issues - the quotes were missing, I had to do a cast to float, and a major issue was that the variable was never defined. Making use fo the log certainly helps here … beginner’s fault :grimacing:

Is there a way to define a variable within a script locally so that it only exists there, that there are no conflicts with potentially equally named variables in other scripts, and to make sure that it is always defined for that script independent of any “global” setups? I am not referring to variables that should be passed to that script, but indeed only ones to be used within the script, like the current volume one here.

Is there a way to define a variable within a script locally

First section on this page.

Thanks, Jason. I am still stuggling (as a newbie) with the basics of setting a specific value to a variable. As some code snippets, I would have assumed that it works like the following, but obviously some major flaws in there as it doesn’t even recognize the service:

script:
  variables:
    var_number: 0.5
  sequence:
    - service: variable.set_variable
      data_template:
        variable: "{{ var_number }}"
        value: 0.3

Would anybody be able to point me to the (probably obvious) mistakes here and how to handle such variables? Unfortunately the “Scripts” link doesn’t explain that and also some additional searching hasn’t led to anything. Thanks again!

Looks like you are mixing up automation/script variables and one of the custom variable integrations. Where did you come up with the set_variable service you tried to use? You also didn’t name your script!

You really need to read the link I sent before and this link carefully. If you’re trying to write scripts and don’t understand the basic format you’re going to be in for a very frustrating time!

Once you’ve read those if you still have questions, fire away…

Here’s a quick example to demonstrate how to change a variable value within a script. Put this in your config, reload your scripts, execute this script, then look at your log to see the result.

script:
  test_variable:
    variables:
      var_number: 0.5
    sequence:
      - variables:
          var_number: 0.3
      - service: system_log.write
        data:
          message: "var_number value: {{ var_number }}"

Thanks again, Jason - now fully clear. Really appreciate your help.

I obviously read the documentation intensively as well as searched the forum but nowhere found how to set these number variables in scripts, therefore fell back to experimenting with a format similar to input_numbers or the custom variables component. Now it is clearer (and obviously much simpler). I understood the example in the “script syntax” documentation originally as how to define the variables and not how to set them within the sequence of the script. I will suggest a slight update to it on GitHub to make it clearer.

The documentation does behind sometimes when things are happening quickly. Script variables have only been around a month or two.

Glad I could help. :slight_smile:

This file no longer exists, could you upload it again? Thanks in advance. :slight_smile:

1 Like

Thanks for the great content here for the google home broadcast.

I’'m stuck on one thing though - where do I store the local MP3 files and how do I reference them? I have tried putting them in the config share, in the local media share, in the www folder, but I cannot get the files to play from this script.

I guess with that comes the question of how to reference local files on the HA installation from a script? If the following line is used:

media_content_id: “{{ states(‘input_text.home_assistant_url_internal’) + ‘/local/sounds/silence-1sec.mp3’ }}”`

Does that mean files go in the www folder in the config share and that /local/sounds are folders that I create and upload files too?
If I don’t use the input_text.home_assistant_url_internal, how to I reference a file path?

I have HA installed in a VM on a Hyper-V VM so it’s a full installation.

Any tips would be greatly appreciated!

The Google Cast only accepts an external url. So you need to use that in your call. The audio files go in the config\www folder. In the external url, the www folder is referred to as ‘local’. They are one and the same folder.

This applies in my situation, YMMV. I would suggest simply replacing your internal url with external one.

Hi @Emphyrio,

Can you share the MP3 files once again?

Here you go, never used this file hoster before so please let me know if it works for you:

1sec.mp3
announcement.mp3

1 Like

Thanks! That worked!

Thanks for that. I think I have almost figured it out. One thing I don’t understand though, the input_text bit. Where does the input_text get defined? Is it a helper, is it from the General configuration of HA, or something else I need to create myself?

Also, when you say external URL, does that mean something that is publicly addressable (so a website on the internet), or just addressable by the Google speaker internally to my network?

Thanks again for your help.

Hi Rob,

About the input_text: this is my way to keep my base_url at a central location. It is not required in any way. My baseurl is: https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.ui.nabu.casa. You can just use one string, e.g.:
media_content_id: "https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.ui.nabu.casa/local/audio/1sec.mp3"

With ‘external url’ I mean an URL that is not in your local network. An URL like this doesn’t seem to work for casting: 192.168.0.10/local/audio/1sec.mp3. Because that is an IP-address on your local network. For casting to work, the URL needs to be reachable from the internet, not just on your local network.

Hope that helps.

1 Like

I’ve developed a script that you guys may like:

2 Likes

Thanks. Works great!

An earlier version of HA allowed this

    service: tts.google_translate_say
    data:
      entity_id: media_player.all
      message: "there is someone at the door"
      volume: 0.7

Now it errors - but if I remove the ‘volume: 0.7’ it works.
There are scripts in this thread which would help if I was addressing a single media_player, recording/restoring the volume, but it is going to be tedious doing that for every media_player in the house.
Added to which media_players which are switched off don’t have a ‘volume_level’ attribute.

If I go simple and just want to output to all media_players - I now have no idea what volume it will come out at.

Any suggestions? Am I missing a simple solution?

Hello

I need to create a script for TTS Cloud announcements, through which to set a certain volume level for the TTS announcement, but at the same time to pause what was playing (if was something playing at that moment) on that media player and after the announcement has been played, to resume what was playing before on that media player, at the same volume level as before the announcement (again if there was something playing at moment of the announcement) and if nothing was playing at the announcement time to restore the previous volume level of that media player.

Could this be possible?
Thanks in advance!