Script for TTS message when conditions are met

Hi,
This is my first post as I am just starting with Home Assistant. I’m making a script that first checks if a number of doors are open for longer than 5 minutes. If that is true then I want to send a tts message to my nest speakers (I made a group). This only must happen if there is at least 1 person in the house otherwise it will only play the message for my dog :slight_smile: and I don’t think he is able to close the doors for me. What I am struggling with is to how to determine that at least 1 person is in the house (out of the 3 people). I would appreciate if someone could help me by pointing me in the right direction. This is the code:

alias: notify.tts.google.say:NOTIFICATION.WHEN.FRONTDOOR.OPEN
sequence:
  - condition: or
    conditions:
      - condition: state
        entity_id: binary_sensor.0xa4c13888dc045dd8_contact
        state: "on"
        for:
          hours: 0
          minutes: 5
          seconds: 0
      - condition: state
        entity_id: binary_sensor.0xa4c1389529eac745_contact
        state: "on"
        for:
          hours: 0
          minutes: 5
          seconds: 0
      - condition: state
        entity_id: binary_sensor.0xa4c138114f444ccf_contact
        state: "on"
        for:
          hours: 0
          minutes: 5
          seconds: 0
  - if:
      - condition: state
        entity_id: person.name_of_person1
        state: home
      - condition: state
        entity_id: person.name_of_person2
        state: home
    then:
      - service: media_player.volume_up
        data: {}
        target:
          entity_id: media_player.all_homespeakers
      - service: tts.google_translate_say
        data:
          cache: false
          entity_id: media_player.homespeaker_living
          message: "De voordeur is langer dan 5 minuten open "
          language: nl
mode: single
icon: mdi:google-assistant

Welcome to the forum. Since you said you were new to HA… Based on what you have posted above, it seems like you may be trying to create an automation, not a script. If that is the case, let us know and we can help you modify what you have as necessary.

As to your question…

You don’t need to check each person entity one at a time, instead you can check the state of the home zone. A zone’s state is the number of known people entities that are currently in that zone, so any value above 0 means someone is home.

- condition: numeric_state
  above: 0
  entity_id: zone.home

Ok, now my code looks like this:

alias: notify.tts.google.say:NOTIFICATION.WHEN.FRONTDOOR.OPEN
sequence:
  - condition: or
    conditions:
      - condition: state
        entity_id: binary_sensor.0xa4c13888dc045dd8_contact
        state: "on"
        for:
          hours: 0
          minutes: 5
          seconds: 0
      - condition: state
        entity_id: binary_sensor.0xa4c1389529eac745_contact
        state: "on"
        for:
          hours: 0
          minutes: 5
          seconds: 0
      - condition: state
        entity_id: binary_sensor.0xa4c138114f444ccf_contact
        state: "on"
        for:
          hours: 0
          minutes: 5
          seconds: 0
  - condition: numeric_state
    entity_id: zone.home
    above: 0
  - service: tts.google_translate_say
    data:
      entity_id: media_player.all_homespeakers
      language: nl
      message: Een of meer deuren aan de voorkant van het huis zijn langer dan 5 minuten open.
mode: single
icon: mdi:google-assistant

Now I’m curious: Is there a way for Google to say how many and which doors are open? It would be much nicer than the message I have now: (translated from dutch) "One or more doors at the front of this house are open for more than 5 minutes. " And let’s say that there is allready something playing on one of the speakers in my speakergroup, wil it stop, play the message and then continue playing?

For your first question about automation or script, my understanding is that a script is more powerfull than an automation but maybe that is not true?

I don’t really know how you would quantify “powerful” in that comparison.

The major difference between the two is that an automation has an internally-defined trigger that makes it run. When a specific event occurs, for example a door is open for 5 minutes, it triggers a sequence of actions.

On the other hand, scripts are just the sequence of actions. They must be initiated by a service call outside of the script itself. On their own they will never perform their action sequence. However, they are a useful tool if you have a sequence of actions that you want to be able to execute from multiple places.

Yes, using Templating you can customize a message to include information about entities dynamically. I don’t have any Google speakers, but here are some examples of ways to template that kind of message. Once you figure out the specifics of how you want the information announced, we can help you figure out how to go from the data you have to what you want.

Do a search of the forum… I think the default behavior for Google speakers is to just play the TTS over top whatever is playing, but I’m sure I’ve seen a thread about ways to work around that.