Need Help with TTS and Time

I am having trouble with the automation and script listed below. I wanted the automation and script to fire every hour between time x and time time y and it does that as expected. However, when speaking the time it always announces it as 59 minutes past the hour as opposed to on the hour. If it fires at 5:00 pm it will announce the time as 4:59, at 6:00 as 5:59 etc. My questions are - can I round the time up to the nearest hour without minutes or seconds, can I format the time to a 12 hour clock instead of 24 hour, and can I include a.m. or p.m.? Thank you all for any/all assistance. Here is the yaml I have -

  • alias: “Time Based Greeting”
    trigger:
    • platform: time_pattern
      hours: “/1”
      minutes: “0”
      seconds: “0”
      condition:
    • condition: state
      entity_id: device_tracker.life360_stephen
      state: “home”
    • condition: time
      after: “07:00:00”
      before: “22:00:00”
      action:
    • service: homeassistant.turn_on
      entity_id: script.stephen_greeting

stephen_greeting:
alias: Stephen Greeting
sequence:
- service: media_player.volume_set
entity_id: media_player.office
data:
volume_level: 0.25
- service: tts.google_translate_say
data_template:
entity_id: media_player.office
message: >
{% if now().strftime("%H")|int < 12 %}
Good Morning Stephen.
{% elif now().strftime("%H")|int < 18 %}
Good Afternoon Stephen.
{% else %}
Good evening Stephen.
{% endif %}
It’s currently {{states.weather.woodland_hills_north.state}} in San Antonio on {{states.sensor.date.state}} at {{states.sensor.time.state}}

One again, thank you for any help.

#
stephen_greeting:
  alias: Stephen Greeting
  sequence:
    - delay: "00:00:02"
    - service: media_player.volume_set
      entity_id: media_player.office
      data:
        volume_level: 0.25
    - service: tts.google_translate_say
      data_template:
        entity_id: media_player.office
        message: >
          {% if now().strftime("%H")|int < 12 %}
          Good Morning Stephen.
          {% elif now().strftime("%H")|int < 18 %}
          Good Afternoon Stephen.
          {% else %}
          Good evening Stephen.
          {% endif %}
          It's currently {{states.weather.woodland_hills_north.state}} in San Antonio on {{states.sensor.date.state}} at {{states.sensor.time.state}}
#
#
- alias: "Time Based Greeting"
  trigger:
    - platform: time_pattern
      hours: "/1"
      minutes: "0"
      seconds: "0"
  condition:
    - condition: state
      entity_id: device_tracker.life360_stephen_mark
      state: "home"
    - condition: time
      after: "07:00:00"
      before: "22:00:00"
  action:
    - service: homeassistant.turn_on
      entity_id: script.stephen_greeting
#

Sorry … did not format the yaml on the other inquiry.

You’re experiencing something I accidentally discovered and that there’s a small discrepancy between what sensor.time reports and the time used by Time Triggers like time and time_pattern (as well as the now() function).

Within your script’s template I suggest you use the now() function instead of sensor.time.

Replace this:

{{states.sensor.time.state}}

with this:

{{now().timestamp() | timestamp_custom('%H:%m')}}

Here’s the resulting final line of the script:

It's currently {{states.weather.woodland_hills_north.state}} in San Antonio on {{states.sensor.date.state}} at {{now().timestamp() | timestamp_custom('%H:%m')}}

You can also streamline it like this:

    - service: tts.google_translate_say
      data_template:
        entity_id: media_player.office
        message: >
          {% set t = now().hour %}
          {% set m = 'Morning' if t < 12 else 'Afternoon' if t < 18 else 'Evening' %}
          Good {{m}} Stephen.
          It's currently {{states.weather.woodland_hills_north.state}} in San Antonio on {{states.sensor.date.state}} at {{states.sensor.time.state}}
1 Like

Thank you for replying. I’ll make the changes you suggest.

Gave it a try and now it is one minute fast. I prefer it that way to one minute slow. Maybe there is no way to get it on the hour/money.

That’s curious.

I couldn’t wait around for it to trigger hourly so I tested it with this automation which executes every minute.

- alias: "Time Test"
  trigger:
    - platform: time_pattern
      minutes: "/1"
      seconds: "0"
  condition:
    - condition: time
      after: "07:00:00"
      before: "22:00:00"
  action:
    - service: persistent_notification.create
      data_template:
        title: "{{'{:02}:{:02}:{:02}'.format(now().hour, now().minute, now().second)}} {{states('sensor.time')}}"
        message: "{{'{:02}:{:02}:{:02}'.format(now().hour, now().minute, now().second)}} {{states('sensor.time')}}"

It displays two times and I compared both to the server’s system clock.

  • The first displayed time, using now(), was perfectly in sync with the system clock (to the second).
  • The second one, using sensor.time, lagged by one minute because it was actually a second or two late (i.e. if sensor.time could show seconds it would show 16:16:58 when now() reports 16:17:00).

You can try it for yourself and confirm that when the notification appears, it occurs the moment the system clock advances to the next minute (and now() shows the correct current time, not early nor late).

1 Like

Thanks again … I will test it out on my end … been having terrible troubles with Netatmo and TTS so I think I’m going to load an old backup and try to get success from there. Thank you for taking the time to look at this for me; very kind of you.

it worked … 16:00 on the nose. is there a place where I can look up format time rules? Instead of the announcement saying 14:00 I would prefer 4:00 pm. Anyway, the main issue is addressed Taras.

Replace the %H with %I to get 12-hour time or preferably with %-I to eliminate zero-padding. For other time-formatting options see:

Thanks once again.

1 Like

There’s one final thing you can do to close out this topic. Please mark my post (the one above) with the Solution tag. Only you, the author of this topic, can do that.

It will automatically place a check-mark next to the topic’s title which signals to others that this topic has an accepted solution. It will also place a link below your first post that leads to the solution. All of this helps other users find answers to similar questions. Thank you.

2 Likes