Generative AI Goodnight message with fallback

I’ve had a good night reminder script for a while (that spins up when we say ‘bedtime’ to google, alexa or just put the house in ‘night’ mode), but I just enhanced it using Generative AI to spice things up a bit. Would love to see what else folks have HA remind them to do before they go to bed. This is using the ‘variables+history’ HACS integration that is fantasic for composing long messages and storing AI responses in. I also have my own custom notifier that distributes the message (under certain conditions) to displays, TTS devices and phone notifications, but that’s a post for another day…

alias: .say unavailable sensors and opened doors and sump pump
sequence:
  - service: variable.update_sensor
    metadata: {}
    data:
      replace_attributes: true
      attributes:
        message: >-
          Good night.  

          It is currently {{state_attr('weather.chezaggar','temperature')}}
          degrees outside and {{states('weather.chezaggar')}}.  Tonight, expect 
                    {{states('sensor.chezaggar_condition_night_0d')}}.

          Before you head up to bed, please
             
          {%-if states('input_text.window_and_door_check') != '' %}
            Check that the {{states('input_text.window_and_door_check')}} are locked.
          {%-endif%}

          {%-if states('sensor.24_hour_sump_pump_watts') | float < 100 and
          states('sensor.24_hour_precipitation') | float > 0.5 and
          states('sensor.sump_pump_monitor') != '' %}
              {{states('sensor.sump_pump_monitor')}}.
           {%-endif%}

           {%-if states('input_boolean.guests_in_basement') == 'on' and (as_timestamp(now()) - as_timestamp(states.light.basement_bedroom_lights.last_changed)) /60 > 300 %}
             Consider turning off the "Guests in Basement" switch as the basement bedroom lights have not been on in the last 5 hours.
           {%-endif%}

           {%-if states('sensor.maggar_surfacelaptop_sessionstate') == 'Unlocked' %}
              Mark's office PC is still on.  Please check it.
           {%-endif%}

           {%-if states('sensor.sensors_unavailable') != '' %}
             The following sensors might be offline and need checking: {{ states('sensor.sensors_unavailable') }}.
           {%-endif%}

           {%-if states('binary_sensor.workday_tomorrow') == 'on' and states('input_boolean.pretenditsweekend') == 'off' and states('binary_sensor.liam_laptop_charging') == "off" and states('person.liam') == 'home' %}
             It doesn't seem like Liam's laptop is plugged in.  Please make sure it is charging!
           {%-endif%} 

           {%-if states('vacuum.roborock_q_revo') != 'docked' and states('vacuum.roborock_q_revo') != 'cleaning' %}
             The main floor Roborock is not docked or responsive.  Please check it.
           {%-endif%}

           Clear the main floor of obstacles and ensure the roborock water tanks are good to go so the floor will be clean in the morning.   
              
      value: "{{now()}}"
    target:
      entity_id: sensor.goodnight_message
  - service: google_generative_ai_conversation.generate_content
    metadata: {}
    data:
      prompt: >-
        Take the following text and summarize it in the style of a caring and
        trustworthy person with a direct manner. Do not include emojis or hash
        tags in your response.


        {{state_attr('sensor.goodnight_message','message')}}
    response_variable: ai_response
    continue_on_error: true
  - service: variable.update_sensor
    metadata: {}
    data:
      replace_attributes: false
      attributes:
        response: "{{ai_response['text']}}"
      value: "{{now()}}"
    target:
      entity_id: sensor.goodnight_message
    continue_on_error: true
  - event: UNotify
    event_data:
      message: |-
        {% if state_attr('sensor.goodnight_message','response') != none %}
           {{state_attr('sensor.goodnight_message','response')}}
        {%else%}
           Note that Google Generative AI was not available for this message.
           {{state_attr('sensor.goodnight_message','message')}}
        {%endif%}   
      message_speak: "yes"
      message_speak_target: kitchen
      tag: goodnight
mode: single
1 Like

Here’s an example of the message and response attributes:

Message:
Good night. It is currently 41 degrees outside and rainy. Tonight, expect Chilly with a couple of showers of rain or snow. Before you head up to bed, please Check that the Family Room Doors, Deck South Doors are locked. Mark’s office PC is still on. Please check it. Clear the main floor of obstacles and ensure the roborock water tanks are good to go so the floor will be clean in the morning.
Response:
Hi there! It’s a chilly 41 degrees outside with some rain or snow expected tonight. Before you head upstairs, please make sure these things are taken care of: * Lock the Family Room Doors and Deck South Doors. * Check if Mark’s office PC is still on and turn it off if it is. * Clear any obstacles from the main floor. * Check the roborock water tanks to ensure they’re full so the floor will be nice and clean in the morning. Thanks for taking care of these small tasks so that you can rest easy tonight. Sleep well!