How to deal with extra variables in a service_template?

I have been off the scene for some months now and I feel I should know this, so apologies in advance.

I have a general notification engine and I can dynamically set the tts service. I’ve just subscribed to Nabu Casa so am trying out the Google Neural Netwoks voices.

I’d like to pass the language and gender but not all tts services accept them so those calls fail.

Specifically, I want to do this:

              - service_template: >
                  tts.{{ states('input_select.announcement_voice') }}_say
                data_template:
                  entity_id: >    
                    {{ media_player }}
                  message: >
                    {{ message }}
                  options:
                    gender: >
                      {{ states('input_select.announcement_gender') }}
                  language: >
                    {{ states('input_select.announcement_language') }}

But google_translate (for example) does not accept options / gender or language so causes an error.

Is there a way around this? I seem to remember the use of none being introduced but I can’t find any reference to it now.

Thanks.

One way around this would be creating two service calls, one for the tts services that accept lamguage and gender and one for the ones that don’t. Then use choose to determine which of the 2 services should be called based on the wngine chosen.

2 Likes

Thanks, yes I considered that but was hoping for a more elegant / economical method.

You can use templating to a higher degree to choose the service you want based on the trigger (or any other state). For example, here’s a simple one that I use:

  - service_template: >
        {% if is_state('input_boolean.block_back_porch_motion_sensor','on' %} timer.start
        {% else %}                                                            timer.finish
        {% endif %}
    entity_id: timer.block_back_porch_motion_sensor

It simply shows how to select a timer.start or timer.finish service based on the triggering entity (in this case, it’s an input_boolean).

But I don’t think this will solve the problem. You need a way to choose whether or not options: and language: are included. I think @Burningstone hit the nail on the head - this is why choose was added, so we can make choices on which action to take based on conditions. In your case, this seems the way to go.