Janet (The Good Place)

Copy and paste below for Corey Greeting to welcome you back home.

  - alias: Corey Greeting
    trigger:
      - platform: state
        entity_id: device_tracker.coreys_iphone_x
        from: 'not_home'
        to: 'home'
        for: '00:01:00'
    action:
      - wait_template: >-
          {{ not is_state('media_player.fire_speaker', 'playing') }}   
      - service: script.janet_speech_engine
        data_template:
          personarriving: >        
            {% set person = "Corey" %}
            {%- macro greeting_sentence(person) -%}
            {{ [
            "Welcome back home " ~ person,
            "Guess who is home " ~ person + " is!",
            person + " is now in the house.",
            "Welcome Home " ~ person + ".  We have missed you. Or at least I did.",
            "Our home is now complete, Rest your head and relax your feet Welcome Back " ~ person,
            "Life is like a song, you’re back where you belong, Welcome home " ~ person,
            "Hey there " ~ person + " Welcome Home!",
            "Knock Knock. Who is There " ~ person + " is!",
            person ~ " You are home!",
            "I know a secret " ~ person + " is home!"
            ] | random }}
            {%- endmacro -%}
            {{greeting_sentence(person)}}
          call_no_announcement: 1

Ok, now thats working. im getting this error.

Log Details (WARNING)
Sun Jul 15 2018 00:37:42 GMT-0400 (Eastern Daylight Time)

Unable to find service notify/janet

janet_text_processing:
  sequence:
    - condition: state
      entity_id: input_boolean.janet_text_notifications
      state: 'on'
       ### change to notify you have notify.janet ### **(Am I supposed to change something here?)**
    - service: notify.Janet
      data_template:
        title: "Janet:"
    message: >-
      {{ speech_message | replace("\n","") | replace("   ","") |
         replace("eight hundred and sixty one thousand","861.000") }}

I figured out the notify.janet had to be changed to notify.corey (sometimes i miss the obvious)

now this error, im looking into it now.

Unable to find service notify/corey
1:18 AM core.py (WARNING)
Error during template condition: UndefinedError: ‘str object’ has no attribute ‘fire_speaker’
1:18 AM helpers/condition.py (ERROR)

see the line

  • service: notify.janet #<=== you have to change this to notify you have example notify.ios_your_iphone or notify.telegram_name_you_have or pushbullet. What notify you have in HA change to according to what you have.

If you don’t have any notify text message. then just turn off janet text notification on frontend of ha.

I do have notify.ios_corey

I havent been able to get zone location other then myself to work. But the service is showing.

I do think the Janet automation isn’t working because of

2018-07-15 21:46:56 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: ‘media_player’ is undefined

Then you just put # in front all the line from service:

 #   - service: notify.Janet
 #     data_template:
 #       title: "Janet:"
 #   message: >-
 #    {{ speech_message | replace("\n","") | replace("   ","") |
 #        replace("eight hundred and sixty one thousand","861.000") }}

OK, I didn’t receive any errors when running the automation, however nothing happened :frowning:

I’m not sure why its giving me so many issues.

I really do appreciate your help, if you dont want to keep suggesting things ill understand and move on from this project.

Automation.yaml. is this correct?

- alias: Corey Greeting
  trigger:
    - platform: state
      entity_id: device_tracker.coreys_iphone_x
      from: 'not_home'
      to: 'home'
      for: '00:01:00'
  action:
    - wait_template: >-
        {{ not is_state('media_player.fire_speaker', 'playing') }}
    - service: script.janet_speech_engine
      data_template:
        personarriving: >
          {% set person = "Corey" %}
          {%- macro greeting_sentence(person) -%}
          {{ [
          "Welcome back home " ~ person,
          "Guess who is home " ~ person + " is!",
          person + " is now in the house.",
          "Welcome Home " ~ person + ".  We have missed you. Or at least I did.",
          "Our home is now complete, Rest your head and relax your feet Welcome Back " ~ person,
          "Life is like a song, you’re back where you belong, Welcome home " ~ person,
          "Hey there " ~ person + " Welcome Home!",
          "Knock Knock. Who is There " ~ person + " is!",
          person ~ " You are home!",
          "I know a secret " ~ person + " is home!"
          ] | random }}
          {%- endmacro -%}
          {{greeting_sentence(person)}}
        call_no_announcement: 1

Happy to help. Yes, what posted above is an automation. You have to put in where you ask your automation file located.

I do its in automations.yaml
So im not sure why it wont work…

Then put this automation in automations.yaml .
So there will be many automations in your automations.yaml.

Check the indent and space from your previous automation inside automations.yaml

example for wrong automation
If in your automations.yaml the space or indent didn’t line up properly. Check configuration will be failed.

- alias: Corey Greeting ### automation 1
  trigger:
    - platform: state
      entity_id: device_tracker.coreys_iphone_x

   - alias: B Greeting ### automation 2
     trigger:
       - platform: state
         entity_id: device_tracker.coreys_iphone_x

the correct automation the space and indent of every automations are the same.

- alias: Corey Greeting ### automation 1
  trigger:
    - platform: state
      entity_id: device_tracker.coreys_iphone_x
- alias: B Greeting ### automation 2
  trigger:
    - platform: state
      entity_id: device_tracker.coreys_iphone_x

Configuration invalid
Invalid config for [automation]: required key not provided @ data[‘action’]. Got None. (See /config/configuration.yaml, line 144). Please check the docs at Automation - Home Assistant
Invalid config for [automation]: required key not provided @ data[‘action’]. Got None. (See /config/configuration.yaml, line 144). Please check the docs at Automation - Home Assistant

thats why im confused, its all setup properly…

This is the lay out in configruation.yaml to tell where your automation is / scroll down see the ADVANCED USAGE

I have been splitting my configuration since almost at the beginning. Cleans it up for sure

ok I got it working finally!

now im trying to get it to work with 6 family members. My automation clears config check, does anybody know what might be wrong?

- id: family_greeting
  alias: Family Greeting
  trigger:
    platform: state
    entity_id: binary_sensor.cameron_home, binary_sensor.corey_home, binary_sensor.courtney_home, binary_sensor.mom_home, binary_sensor.morgan_home, binary_sensor.wendy_home
    from: 'off'
    to: 'on'
    for: '00:01:00'
  action:
    - wait_template: "{{ is_state('script.janet_speech_engine', 'off') }}"
    - service: script.janet_speech_engine
      data_template:
        message: >
          {% if (states.binary_sensor.cameron_home.last_changed -
                 states.binary_sensor.corey_home.last_changed -
                 states.binary_sensor.courtney_home.last_changed -
                 states.binary_sensor.mom_home.last_changed -
                 states.binary_sensor.morgan_home.last_changed -
                 states.binary_sensor.wendy_home.last_changed)
                .total_seconds()|abs < 60 %}
            {% set p = state_attr('binary_sensor.cameron_home','friendly_name')
                     ~ ' and '
                     ~ state_attr('binary_sensor.corey_home','friendly_name') %}
                     ~ ' and '
                     ~ state_attr('binary_sensor.courtney_home','friendly_name') %}
                     ~ ' and '
                     ~ state_attr('binary_sensor.mom_home','friendly_name') %}
                     ~ ' and '
                     ~ state_attr('binary_sensor.morgan_home','friendly_name') %}
                     ~ ' and '
                     ~ state_attr('binary_sensor.wendy_home','friendly_name') %}
          {% else %}
          {% set person = trigger.to_state.name %}
          {% endif %}
          {{ [
            "Welcome back home " ~ person,
            "Guess who is home. " ~ person ~ " is!",
            person ~ " is now in the house.",
            "Welcome Home " ~ person ~ ".  We have missed you. Or at least I did.",
            "Our home is now complete, Rest your head and relax your feet, Welcome Back " ~ person,
            "Life is like a song, you’re back where you belong, Welcome home " ~ person,
            "Hey there " ~ person ~ " Welcome Home!",
            "Knock Knock. Who is There? " ~ person ~ " is!",
            person ~ ", you are home!",
            "I know a secret. " ~ person + " is home!"
          ] | random }}

Can we make the automation shorter like this. As you already trigger the state of the binary_sensors in automation.

- id: family_greeting
  alias: Family Greeting
  trigger:
    platform: state
    entity_id: binary_sensor.cameron_home, binary_sensor.corey_home, binary_sensor.courtney_home, binary_sensor.mom_home, binary_sensor.morgan_home, binary_sensor.wendy_home
    from: 'off'
    to: 'on'
    for: '00:01:00'
  action:
    - wait_template: "{{ is_state('script.janet_speech_engine', 'off') }}"
    - service: script.janet_speech_engine
      data_template:
        message: >
          {% set person = trigger.to_state.attributes.friendly_name %}
          {{ [
            "Welcome back home " ~ person,
            "Guess who is home. " ~ person ~ " is!",
            person ~ " is now in the house.",
            "Welcome Home " ~ person ~ ".  We have missed you. Or at least I did.",
            "Our home is now complete, Rest your head and relax your feet, Welcome Back " ~ person,
            "Life is like a song, you’re back where you belong, Welcome home " ~ person,
            "Hey there " ~ person ~ " Welcome Home!",
            "Knock Knock. Who is There? " ~ person ~ " is!",
            person ~ ", you are home!",
            "I know a secret. " ~ person + " is home!"
          ] | random }}

The config comes back good, but nothing plays…

The binary_sensor itself has fast trigger for ‘on’ and ‘off’

It is better to change trigger of ‘binary_sensor’ to ‘device_tracker’ and state from ‘on’ and ‘off’ to ‘home’ and ‘not_home’

also remove the wait_template

Actually the janet_speech engine needed reworked a bit to work with the automation.
The reason I do it this was is I actually have my iOS device Home app trigger a boonlean switch this way my phone actually tells home assistant that im home to trigger the automation. It works so much better then the HA app does for tracking. Thanks for all your help getting this going.

is there anything else it can do?

script.yaml

janet_speech_engine:
  sequence:
    - wait_template: >-
        {{ not is_state(states('input_select.janet_notification_media_player'), 'playing') }}
    - service: script.janet_notification_hub
      data_template:
        speech_message: "{{ message.replace('PERSON',person) }}"
        media_player: "{{ states('input_select.janet_notification_media_player') }}"

######################################################################################################
##  janet_notification_hub is the bridge between the speech_engine and the processing units.        ##
######################################################################################################

janet_notification_hub:
  sequence:
    - service: script.janet_speech_processing
      data_template:
        media_player: "{{ media_player }}"
        speech_message: "{{ speech_message }}"
    - service: script.janet_text_processing
      data_template:
        speech_message: "{{ speech_message }}"

janet_speech_processing:
  sequence:
    - condition: and
      conditions:
        - condition: or
          conditions:
            - condition: time
              after: '07:00:00'
              before: '23:00:00'
            - condition: state
              entity_id: input_boolean.janet_alert_mode
              state: 'on'
        - condition: state
          entity_id: input_boolean.janet_speech_notifications
          state: 'on'
        - condition: or
          conditions:
            - condition: state
              ### change the condition to what you need group.location ###
              entity_id: group.all_devices
              state: 'home'
            - condition: state
              entity_id: input_boolean.janet_guest_mode
              state: 'on'
    - wait_template: "{{ not is_state(media_player, 'playing') }}"
      timeout: 00:00:30

    - service: media_player.volume_set
      data_template:
        entity_id: "{{ media_player }}"
        volume_level: "{{ states('input_number.janet_notification_volume') }}"
    - service: tts.google_say
      data_template:
        entity_id: "{{ media_player }}"
        message: "{{ speech_message }}"

janet_text_processing:
  sequence:
    - condition: state
      entity_id: input_boolean.janet_text_notifications
      state: 'on'
       ### change to notify you have notify.janet ###
  #  - service: notify.ios_corey
  #    data_template:
  #      title: "Janet:"
  #      message: >-
  #        {{ speech_message | replace("\n","") | replace("   ","") |
  #           replace("eight hundred and sixty one thousand","861.000") }}

And the automation.yaml

- id: family_greeting
  alias: Family Greeting
  trigger:
    platform: state
    entity_id:
      - binary_sensor.cameron_home
      - binary_sensor.corey_home
      - binary_sensor.courtney_home
      - binary_sensor.mom_home
      - binary_sensor.morgan_home
      - binary_sensor.wendy_home
    from: 'off'
    to: 'on'
    for: '00:00:05'
  action:
    - wait_template: "{{ is_state('script.janet_speech_engine', 'off') }}"
    - service: script.janet_speech_engine
      data_template:
        person: >
          {{ trigger.to_state.name }}
          {%- for s in states.binary_sensor
                |rejectattr('entity_id','eq',trigger.entity_id)
                |selectattr('state','eq','on')
                if '_home' in s.entity_id and
                   (now() - s.last_changed).total_seconds() < 60 -%}
            {{ (' and ' if loop.last else ', ') ~ s.name }}
          {%- endfor %}
        message: >
          {{ ["Welcome back home PERSON",
              "Guess who is home. PERSON is!",
              "PERSON is now in the house.",
              "Welcome Home PERSON.  We have missed you. Or at least I did.",
              "Our home is now complete, Rest your head and relax your feet, Welcome Back PERSON",
              "Life is like a song, you’re back where you belong, Welcome home PERSON",
              "Hey there PERSON Welcome Home!",
              "Knock Knock. Who is There? PERSON is!",
              "PERSON, you are home!",
              "I know a secret. PERSON is home!"
             ] | random }}

If everything is correct. Then I would at the binary_sensor into a group.
- binary_sensor.cameron_home
- binary_sensor.corey_home
- binary_sensor.courtney_home
- binary_sensor.mom_home
- binary_sensor.morgan_home
- binary_sensor.wendy_home

then in janet_speech_processing

        - condition: state
          ### change the condition to what you need group.location ###
          entity_id: group.all_devices  <== change to the group you create above.
          state: 'home'  <=== change the state to 'on'

then in automation family_greeting I would remove this line

  • wait_template: “{{ is_state(‘script.janet_speech_engine’, ‘off’) }}”

I could but wouldnt that take the personalized greeting away, doing it this way it says

ie.

Welcome, corey, wendy and courtney if 3 of us come come together.

or just welcome cameron if he comes home by himself

in janet_speech_processing. the conditions are ‘and’ then ‘or’ which in ‘or’ the entity_id: group.all_devices <== in state of ‘home’ which you use binary_sensor the state is ‘on’ and ‘off’ this didn’t match. So you have to turn on input_boolean.janet_guest_mode all the time.

for automation
It read all binary_sensors that have change the state to ‘on’ and you have put in how to speak person name (the part person in your automation quite confusing me).