What is wrong with this time announcing automation?

I wrote this automation to announce the time in 24-hour format. It triggers, but does not announce the time. The action is so:

service: mqtt.publish
data:
  topic: hermes/dialogueManager/endSession
  payload_template: |-
    {% if now().strftime("%M") | int == 0 %} # zero minutes
      {% if now().strftime("%-H") | int < 10 %} # zero minutes, hours < 10
        {"sessionId": "{{ trigger.event.data._intent.sessionId }}", "text": "The time is zero {{ now().hour }} hundred "}
      {% else %} # minutes zero, hours => 10
        {"sessionId": "{{ trigger.event.data._intent.sessionId }}", "text": "The time is {{ now().hour }} hundred "}
      {% endif %}
    {% elif now().strftime("%M") | int < 10 %} # minutes < 10
      {% if now().strftime("%-H") | int < 10 %} #minutes < 10, hours < 10
        {"sessionId": "{{ trigger.event.data._intent.sessionId }}", "text": "The time is zero {{ now().hour }} zero {{ now().minute }} "}
      {% else %} # minutes < 10, hours => 10
        {"sessionId": "{{ trigger.event.data._intent.sessionId }}", "text": "The time is {{ now().hour }} zero {{ now().minute }} "}
      {% endif %}
    {% else %}
      {% if now().strftime("%-H") | int < 10 %} #minutes => 10, hours < 10
        {"sessionId": "{{ trigger.event.data._intent.sessionId }}", "text": "The time is zero {{ now().hour }} {{ now().minute }} "}
      {% else %} # minutes and hours => 10
        {"sessionId": "{{ trigger.event.data._intent.sessionId }}", "text": "The time is {{ now().hour }} {{ now().minute }} "}
      {% endif %}
    {% endif %}

i can’t find my mistake. Anyone see it?

I didn’t take the time to try to fully understand your template, but one thing that seems obviously wrong is the comments. That’s not how to add comments in a template. It should be like this:

    {% if now().strftime("%M") | int == 0 %} {# zero minutes #}

See: Jinja comments

In addition to what Phil said, you can make this template a bit more readable with a singular result as the output instead of replicating the same json dictionary multiple times. This reduces the chance for errors. You can also just get the hours or minutes without needing to ‘convert’ the value with strftime. Lastly, you can get rid of the nested if statements. Just look at hour and minute separetly.

{% set sessionId = trigger.event.data._intent.sessionId %}
{% set t = now() %}
{% if t.minute == 0 %}
  {% set minutes = 'hundred' %}
{% elif t.minute < 10 %}
  {% set minutes = 'zero ' ~ t.minute %}
{% else %}
  {% set minutes = t.minute %}
{% endif %}
{% if t.hour < 10 %}
  {% set hours = 'zero ' ~ t.hour %}
{% else %}
  {% set hours = t.hour %}
{% endif %}
{"sessionId": "{{ sessionId }}", "text": "The time is {{ hours }} {{ minutes }}" }

You can also utilize in line if statements or the immediate if function. It works like the @if macro in excel

iff( statement, true-output, false-output)

{% set sessionId = trigger.event.data._intent.sessionId %}
{% set t = now() %}
{% if t.minute == 0 %}
  {% set minutes = 'hundred' %}
{% else %}
  {% set minutes = iif(t.minute < 10, 'zero ', '') ~ t.minute %}
{% endif %}
{% set hours = iif(t.hour < 10, 'zero ', '') ~ t.hour %}
{"sessionId": "{{ sessionId}}", "text": "The time is {{ hours }} {{ minutes }}" }

Lastly, you can take a completely different route that’s super short for the sake of being short.

{% set sessionId = trigger.event.data._intent.sessionId %}
{% set a, b, c, d = now().strftime("%H%M") %}
{% set minutes = 'hundred' if c ~ d == '00' else c.replace('0', 'zero ') ~ d %}
{% set hours = a.replace('0', 'zero ') ~ b %}
{"sessionId": "{{ sessionId }}", "text": "The time is {{ hours }} {{ minutes }}" }

The service call you posted above appears to be the same challenge you posted here where I solved it with a three-line template (from your original fourteen-line template).

What requirements changed so that you chose to forego it in favor of using the nineteen-line template you posted above?

1 Like

Thanks, I see that, although I just inserted a # comment in a working automation without the brackets and it ran. Curious…

This is very helpful, especially about how to use ‘set’. Thanks very much.