TTS - telling the time

i have this scrip

message: oven is on for {{ (as_timestamp(now()) - as_timestamp(states.automation.oven_warning.attributes.last_triggered))| timestamp_custom("%M") }} minutes!

for tigger at 5 mins, i got 05 mins

what should i do?

I assume you don’t like the zero???

How about:

message: oven is on for {{ (as_timestamp(now()) - as_timestamp(states.automation.oven_warning.attributes.last_triggered))//60 }} minutes!
1 Like

Thanks that work very well

message: oven is on for {{ (as_timestamp(now()) - as_timestamp(states.automation.oven_warning.attributes.last_triggered))| round(0) //60}} minutes!

1 Like

hi @pnbruckner seems the new Sonos setup have broken the time syntax

Any idea on how to tell HA that its a syntax and not having it spelling it out… :slight_smile:

You’re going to have to fill in some details as to what used to work and what’s different now. I don’t use sonos or TTS, so I have no idea what might have changed.

sorry before I used your excellent syntax
message: "Victoria og Thomas Nu er det sengetid klokken er {{ now().strftime('%H %M') }}"

But now it doesnt recognize the syntax it just speaks the code?
so maybe its just the {{ that have somehow changed?

Please post the entire automation and/or script as it/they exist now.

Yes of course sorry…

script:

# Sonos https://www.home-assistant.io/cookbook/sonos_say/
  sonos_say:
    alias: "Sonos TTS script"
    sequence:
     - service: sonos.snapshot
       data_template:
         entity_id: "{{ sonos_entity }}"
     - service: sonos.unjoin
       data_template:
         entity_id: "{{ sonos_entity }}"
     - service: media_player.volume_set
       data_template:
         entity_id: "{{ sonos_entity }}"
         volume_level: "{{ volume }}"
     - service: tts.google_say
       data_template:
         entity_id: "{{ sonos_entity }}"
         message: "{{ message }}"
     - delay: "{{ delay }}"
     - service: sonos.restore
       data_template:
         entity_id: "{{ sonos_entity }}"



action:
 - service: script.sonos_say
   data:
     sonos_entity: media_player.sonos_koekken
     volume: 0.5
     message: "Victoria og Thomas Nu er det sengetid klokken er {{ now().strftime('%H %M') }}"
     delay: '00:00:05'

In your automation action you need to use data_template instead of data.

Like you had before:

    action:
     - service: script.turn_on
       entity_id: script.say
       data_template:     <============
         variables:
           where: 'kkken'
           what: 'Klokken er nu {{ now().hour}} {{ "%0.02d" | format(now().strftime("%-M") | int) }}'

Thanks allot @pnbruckner I have been trying so many things, that i completely missed that (Had to change all my scripts to the new Sonos syntax, another breaking change)

@pnbruckner one last question - of topic, I am trying to update my old Sonos TTS script to the new above, and I know you don’t have Sonos but maybe you can see something I can’t

First just a normal text input variable no change:

# Announcement on Sonos
input_text:
  announce_sonos:
    name: Sonos TTS
    initial: Some Text

then the new script:

# Announce TTS input over Sonos
  - alias: Announce TTS Input Over Sonos
    hide_entity: true
    trigger:
      - platform: state
        entity_id: input_text.announce_sonos
    action:
     - service: script.turn_on
       entity_id: script.sonos_say
       data_template:
         sonos_entity: media_player.sonos_koekken
         volume: 0.5
         message: "{{ states.input_text.announce_sonos.state }}"
         delay: '00:00:05'

The old one:

## Announce TTS input over Sonos
  - alias: Announce TTS Input Over Sonos
    hide_entity: true
    trigger:
      - platform: state
        entity_id: input_text.announce_sonos
    action:
     - service: script.turn_on
       entity_id: script.say
       data_template:
         variables:
## Remember to check that the sonos media_player name is correct below!
           where: 'sonos_koekken'
           what: "{{ states.input_text.announce_sonos.state }}"

And the old Sonos script now obsolete and a little long (Replaced with the above in my former post):

 # > SONOS TTS script to speak with text-to-speech
  say:
    alias: SONOS TTS
    sequence:
      - service: sonos.snapshot
        data_template:
          entity_id: "{{ 'sonos.' ~ where }}"
          with_group: yes
      - service: sonos.unjoin
        data_template:
          entity_id: "{{ 'sonos.' ~ where }}"
      - service: media_player.volume_set
        data_template:
          entity_id: "{{ 'sonos.' ~ where }}"
          volume_level: 0.45
      - service: tts.google_say
        data_template:
          entity_id: "{{ 'sonos.' ~ where }}"
          message: "{{ what }}"
          language: 'da'
# Add a delay (default 1) in a length which you think fits best for all the things you want to say over TTS
      - delay:
          seconds: 1
      - delay: >-
          {% set duration = states.media_player[where].attributes.media_duration %}
          {% if duration > 0 %}
            {% set duration = duration - 1 %}
          {% endif %}
          {% set seconds = duration % 60 %}
          {% set minutes = (duration / 60)|int % 60 %}
          {% set hours = (duration / 3600)|int %}
          {{ [hours, minutes, seconds]|join(':') }}
      - service: media_player.sonos_restore
        data_template:
          entity_id: "{{ 'media_player.' ~ where }}"
          with_group: yes

You need to be careful how you invoke a script. There are two ways, and each way passes variables differently. You should change this:

    action:
     - service: script.turn_on
       entity_id: script.sonos_say
       data_template:
         sonos_entity: media_player.sonos_koekken
         volume: 0.5
         message: "{{ states.input_text.announce_sonos.state }}"
         delay: '00:00:05'

to this:

    action:
     - service: script.sonos_say
       data_template:
         sonos_entity: media_player.sonos_koekken
         volume: 0.5
         message: "{{ states.input_text.announce_sonos.state }}"
         delay: '00:00:05'

See this for more details.

Thanks I will look into this, doesn’t seem to be a quick fix LOL

Hi,

I have this:

data_template:
  message: 'The time is {{now().strftime(''%H'')}} o clock.'
entity_id: media_player.google_home_mini
service: tts.google_translate_say

And my problem is in the morning get this: “The time is 08 o clock.”

How can do this without 0?

Try this It’s 12 Hour and does not need the o’clock as google does that part quite well.

        - service: tts.google_translate_say
          entity_id:
            - media_player.google_kitchen
          data_template:
            message: "Its {{ now().strftime('%-I:%M') }}."
1 Like

I do this.
(I’m not posting this because I think it is clever. On the contrary, there has to be a better way to achieve what I’m doing here in a lot less lines :wink: )

{% set message = '' %}

{#               #}
{# DATE AND TIME #}
{#               #}
{% set hrs = (now().hour) %}
{% if hrs > 12 %}
  {% set hrs = hrs - 12 %}
{% endif %}
{% set hrs_s = hrs | string %}

{% set min = (now().minute) %}
{% set min_s = min | string  %}

{% set day = now().strftime(" on %A the ") %}

{% if min == 0 %}
  {% set message = message +  hrs_s + " o'clock" + day %}
{% elif min in (5, 10, 20, 25) %}
  {% set message = message + min_s + ' past ' + hrs_s + day %}
{% elif min in (35, 40, 50, 55) %}
  {% set message = message + (60 - min) | string + ' to ' + (hrs + 1) | string + day %}
{% elif min == 15 %}
  {% set message = message + 'quarter past ' + hrs_s + day%}
{% elif min == 30 %}
  {% set message = message + 'half past ' + hrs_s + day %}
{% elif min == 45 %}
  {% set message = message + 'quarter to ' + (hrs + 1) | string + day %}
{% elif min == 1 %}
  {% set message = message +  min_s + ' minute past ' + hrs_s + day %}
{% elif min == 59 %}
  {% set message = message + (60 - min) | string + ' minute to ' + (hrs_s + 1) | string + day %}
{% elif min < 30 %}
  {% set message = message + min_s + ' minutes past ' + hrs_s + day %}
{% elif min > 30 %}
  {% set message = message + (60 - min) | string + ' minutes to ' + (hrs + 1) | string + day %}
{% endif %}

{% set message = message + now().day | string %}

{% if now().day in (1, 21, 31) %}
  {% set message = message + 'st ' %}
{% elif now().day in (2, 22) %}
  {% set message = message + 'nd ' %}
{% elif now().day in (3, 23) %}
  {% set message = message + 'rd ' %}
{% else %}
  {% set message = message + 'th ' %}
{% endif %}

{% set message = 'It is ' + message + 'of ' + now().strftime("%B") + ',. ' %}

{{ message }}

Ok, I found the solution, just put ‘-’ before ‘H’ and now it is working without zero.

data_template:
  message: 'The time is {{now().strftime('%-H')}} o clock.'
entity_id: media_player.google_home_mini
service: tts.google_translate_say

try this

I just replace the zero with an “oh” in anything less than 10 so "10 oh five PM instead of “ten zero five pm” everything else sounds OK, a lot less coding :slight_smile:

I did something similar to get the response sounding more like a human being, including additional checks for ‘midnight’, ‘midday’, ‘morning’, ‘afternoon’ and ‘evening’ (the latter after lengthy philosophical discussion about when the afternoon actually becomes the evening). 39 lines of code and counting…

1 Like

Below is my simple code that will provide a greeting and then announce the time.
My morning starts early that’s why I have it at 4AM.

data:
  type: tts
target:
  - media_player.office
message: >-
  '{% if now().strftime('%H')|int < 12 and now().strftime('%H')|int > 4 %}
      Good morning
   {% elif now().strftime('%H')|int >= 12 and now().strftime('%H')|int < 16 %}
      Good afternoon
   {% else %}
      Good evening
   {% endif %}. The current time is {{ now().strftime('%I %M %p') }}.