Send a TTS greetings to whom opened the door after arriving home

Tags: #<Tag:0x00007fc4110aae60> #<Tag:0x00007fc4110aad98>

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

With this blueprint, you are able to send a TTS greeting using tts.say_google service to any media_player with the name of the person that arrived in less than the configured minutes.
It also allows you to set a condition to run it and it can also do other actions after the execution.

Thanks for any feedback, that is my first blueprint.

3 Likes

If I want to play a message for only one person, I don’t know how to set it up. You cannot save without the second message.
If there is only one person will it work?
I have no idea how to fulfill the condition. If I leave true is it good?
You can provide a pattern setting. I wanted to test but it doesn’t work
Thank you very much

Thanks for this.

Would be good to be able to select a motion sensor instead of a door sensor :+1:

If you have only one person, you can put the same messages in both boxes. The blueprint will use the single-person message if there is only one person arriving.

You can leave as True without any issue. That can be used to check whenever you wish the message to be played. Lets think if you wish to play only when your player is in idle state. You can use something like:

{{ is_state('mediaplayer.mymedia','idle') }}

Good, I’ll update it for motion sensor as well.

@jackpea I’m unable to set multiple classes for a binary_input. A workaround is change the class from door to motion at the blueprint.
It will be something like this:

blueprint:
  name: TTS on door opening with persons at home
  description: Sends TTS message to a media_player when door is oppened with the person
    at home
  domain: automation
  input:
    door_sensor:
      name: Door sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    persons:
      name: Persons
      selector:
        entity:
          domain: person
    minutes:
      name: Minutes
      description: Minutes between person at home and door trigged
      default: 5
      selector:
        number:
          min: 1.0
          max: 30.0
          step: 1.0
          unit_of_measurement: min
          mode: slider
    message-single:
      name: Message single person
      description: 'Message to salute single person. One possible message per line.
        Use <person> tag to be replaced by person name.

        '
      selector:
        text:
          multiline: true
    message-multiple:
      name: Message multiple persons
      description: 'Message multiple persons. One possible message per line. Use <persons>
        tag to be replaced by person name.

        '
      selector:
        text:
          multiline: true
    persons-concat:
      name: Text to concatenate the persons name
      description: String to be used as concatenate between names
      default: ' and '
      selector:
        text:
          multiline: false
    target:
      name: Target devices
      description: Target devices to play the TTS message
      selector:
        entity:
          domain: media_player
    condition:
      name: Condition to run
      description: 'Add a value_template value for major condition to execute the automation'
      default: 'True'
      selector:
        text:
    actions:
      name: Additional actions
      description: 'After the execution of the automation, run those actions.'
      default: []
      selector:
        action: {}
  source_url: https://gist.github.com/rokam/22fd6e497fbe61478fda4d0fc8215abf
variables:
  persons_str: !input 'persons'
  persons_list: '{{ persons_str.split('','') | map(''trim'') | list }}'
  timeout: !input 'minutes'
  concat_str: !input 'persons-concat'
  addactions: !input actions
  persons_count: "{%- set first = true -%} {%- set data = namespace(nomes=[]) -%}\
    \ {%- for person in persons_list -%}\n  {%- if is_state(person, 'home') and (now()\
    \ - states[person].last_changed).total_seconds() < timeout*60 -%}\n    {%- set\
    \ data.nomes = data.nomes + [states[person].name] -%}\n  {%- endif -%}\n{%- endfor\
    \ -%} {{ data.nomes }}\n"
  persons_names: '{%- set finalstr = '', ''.join(persons_count) -%} {{ concat_str.join(finalstr.rsplit('',
    '',1)) }}

    '
  single_msg: !input 'message-single'
  multiple_msg: !input 'message-multiple'
trigger:
- platform: state
  entity_id: !input 'door_sensor'
  to: 'on'
action:
- condition: template
  value_template: !input 'condition'
- condition: template
  value_template: "{{ persons_count|length > 0 }}"
- service: tts.google_say
  data:
    entity_id: !input 'target'
    message: "{%- set single_lst = single_msg.split('\\n') -%}\n{%- set multiple_lst\
      \ = multiple_msg.split('\\n') -%}\n{%- if persons_count|length > 1 -%}\n  {{\
      \ multiple_lst | random | replace('<persons>', persons_names) }}\n{%- elif persons_count|length > 0 -%}\n\
      \  {{ single_lst | random | replace('<person>', persons_names) }}\n{%- endif\
      \ -%}\n"
- condition: template
  value_template: "{{ addactions|length > 0 }}"
- choose: []
  default: !input 'actions'
mode: single
1 Like

If I run the automation manually I see that it makes a check on number of persons and it doesn’t continue. Is this expected?
Thanks for this template

@danielo515 if there is no one that have arrived home in less then 5 minutes (or other value that you’ve set), there won’t be anyone to greet and the script ends.

Just tried the script with me actually coming and it just didn’t work. Maybe it conflicted with another script that I have that does exactly the same (but doesn’t wait for the door open event). I’ll try to deactivate it and see if yours work.

Look at the automation trace. Look what happened at the execution.

Seems that people count is always 0. Why can be that ? I’m using the phone integration and it works quite reliably

Can you share your blueprint config?

Dear rokam,
If I don’t misunderstand there is the possibility to add multiple person names to the relative entry but I don’t know how to do it. Can you explain how I can do this ?

You just need to separate them by a comma.

eg.

Would be awesome if you can select the TTS service. tts.google_say or tts.cloud_say for example.

I adjusted the blueprint for myself since I use tts.cloud_say but I think it should not be to difficult to change the blueprint to select the different options.

1 Like

Thank you @rokam. I defined person like you suggested but now I have another problem because every time the automation triggers I see this message on the log

Sorry, text to speech can only be called with the notify.alexa_media service. Please see the alexa_media wiki for details.https://github.com/custom-components/alexa_media_player/wiki/Configuration%3A-Notification-Component#use-the-notifyalexa_media-service

Can you help me ?

The Alexa media player does not allow TTS. This is a limitation of Alexa.

However, it should be possible to make a similar make a blueprint with the option to select the various “notify.alexa_media_(suchandsuch)” to accomplish the same.
I think it would be too complicated to add that in this blueprint, but who knows what @rokam is willing and able to do.

I was going to say the same.
But I don’t understand why you think it may be difficult?
This is what it is required for alexa:

action:
  - service: notify.alexa_media
    data:
      message: Bienvenido de nuevo a casa
      target:
        - media_player.echo_1

And this is what this blueprint does:

  - service: tts.google_say
    data:
      entity_id: media_player.echo_1
      message: 'the message logic'

Quite similar, so i don’t see why it can not be templated

“too complicated” doesn’t mean difficult. I for one wouldn’t go through the effort since I don’t have any Alexa’s. Plus it’s easier to just make a separate blueprint Alexa-based. But as I said: “but who knows what @rokam is willing and able to do.”