Looking to send notification A containing time helper and notification B if time was more than 2 hours ago

New to HA so bear with me. I’ve got a date time helper being set with time and wish to have a script that’ll send a notification containing that time but if that time was more than two hours ago send that time but append the message with some additional text.

Basically this is a way to make sure my kids take the dog out. We ask Alexa when a dog door was opened and now want to Mae sure they know when it’s been more than enough time that the dog needs to go out. Thanks.

Welcome to the Forums!

How are you planning on firing the script(s)? From your post it’s a bit confusing whether you plan on calling the script via a voice command or if it should be an automation trigger by the time helper. Both are doable, we just need a clear point to take off from.

Generally speaking, there are a few methods to have scripts or automations with branched action paths. The first (and often easier method for new users) is to use Choose or If/Then actions. A second is to use templates.

It’s going to be an Alexa routine. Currently we just have Alexa telling us the time the door was last opened but hoping to add some logic to it and if the time was more than two hours ago have Alexa say something like “the door was last opened at X o’clock and the dog needs to go out”.

Please take a look at Asking Good Questions

The following is based on a lot of assumptions:

alias: Query-Alexa-Door Last Opened
sequence:
  - variables:
      last_opened: >
        {{ states('input_datetime.door_last_opened') | as_datetime | as_local }}
  - choose:
      conditions:
        - alias: "If door hasn't been opened in more than 2 hours"
          condition: template
          value_template: >
            {{ now() - last_opened > timedelta(hours=2) }}
      sequence:
        - service: notify.alexa_media_last_called
          data: 
            data:
              type: tts
            message: It's been more than 2 hours since the dog was taken out.
    default:
      - service: notify.alexa_media_last_called
        data:
          message: 'The door was last opened at {{ last_opened.strftime("%-I:%M %p") }}' 
          data:
            type: tts
mode: single

Thanks for your help. So I tried this out substituting my entity names coming up with this but getting an error.

alias: Query-Alexa-Door Last Opened
sequence:
  - variables:
      last_opened: >
        {{ states('input_datetime.dogtime') | as_datetime | as_local }}
  - choose:
      conditions:
        - alias: "If door hasn't been opened in more than 2 hours"
          condition: template
          value_template: >
            {{ now() - last_opened > timedelta(hours=2) }}
      sequence:
        - service: notify.alexa_media_quiet
          data: 
            data:
              type: tts
            message: It's been more than 2 hours since the dog was taken out.
    default:
      - service: notify.alexa_media_quiet
        data:
          message: 'The door was last opened at {{ last_opened.strftime("%-I:%M %p") }}' 
          data:
            type: tts
mode: single

Not sure if the tz in the error is regarding time zone or not or how to correct this.

What type of input datetime is input_datetime.dogtime ? The template is based on the assumption that it would be contain both date and time… if you are using a time-only input datetime you will need to change the variables section:

  - variables:
      last_opened: >
        {{ today_at(states('input_datetime.dogtime')) }}

It is just time (sorry I’ll make sure posts in the future are more forthcoming and helpful). I e adjusted the code as you mentioned but still get an error.

alias: Query-Alexa-Door Last Opened (Duplicate)
sequence:
  - variables:
      last_opened: |
        {{ today_at(states('input_datetime.dogtime')) }}
  - choose:
      conditions:
        - alias: If door hasn't been opened in more than 2 hours
          condition: template
          value_template: |
            {{ now() - last_opened > timedelta(hours=2) }}
      sequence:
        - service: notify.alexa_media_quiet
          data:
            data:
              type: tts
            message: It's been more than 2 hours since the dog was taken out.
    default:
      - service: notify.alexa_media_quiet
        data:
          message: The door was last opened at {{ last_opened.strftime("%-I:%M %p") }}
          data:
            type: tts
mode: single

There seems to be something very weird going on with the time templates in scripts and automations. I’ve tried 6 different variations… all the templates render properly in the Template editor, but at least one part always fails in scripts or automations.

I was able to get it working 2 ways…

by compacting it into a single variable:

alias: Query-Alexa-Door Last Opened
sequence:
  - variables:
      message: |
        {% set dogtime = states('input_datetime.dog_time') %}
        {% if now() > ( today_at( dogtime ) + timedelta(hours=2)) %}
          It's been more than 2 hours since the dog was taken out.
        {% else %}
          The door was last opened at {{ dogtime }}
        {% endif %}
  - service: notify.alexa_media_quiet
    data:
      message: "{{ message }}"
      data:
        type: tts
mode: single

Or by getting rid of the variable:

alias: Query-Alexa-Door Last Opened
sequence:
  - choose:
      conditions:
        - alias: If door not opened in more than 2 hours
          condition: template
          value_template: |
            {{ now() > (today_at(states('input_datetime.dog_time')) 
            + timedelta(hours=2)) }}
      sequence:
        - service: notify.alexa_media_quiet
          data:
            data:
              type: tts
            message: |
              It's been more than 2 hours since the dog was taken out.
    default:
      - service: notify.alexa_media_quiet
        data:
          message: |
            The door was last opened at {{ states('input_datetime.dog_time') }}
          data:
            type: tts
mode: single
1 Like

@Didgeridrew thanks so much for your help and patience. The logic works fine but one last question. Is there a way to have the time read out in 12 hour AM/PM format? As it is now it comes out like this?

Sure, you just need to replace it in the message with:

{{ today_at(states('input_datetime.dog_time')).strftime('%-I:%M %p') }}
1 Like

Thank you so much. I don’t come from a coding background so the templating is still something I’m trying to learn. Works perfect and greatly appreciate your help.