Time comparison in template

In an automation template, I want to format and pass the time to rhasspy. This works:

service: mqtt.publish
data:
  topic: hermes/dialogueManager/endSession
  payload_template: >-
    {"sessionId": "{{ trigger.event.data._intent.sessionId }}", "text": "The time is
    {{ now().hour }} {{ now().minute }} "}

but is not properly formatted. I tried this:

service: mqtt.publish
data:
  topic: hermes/dialogueManager/endSession
service: mqtt.publish
data:
  topic: hermes/dialogueManager/endSession
  {% set minutes = as_timestamp(now()) | timestamp_custom('%M') %}
  {% if strptime(minutes, '%M') == strptime('00', '%M') %}
      payload_template: >-
        {"sessionId": "{{ trigger.event.data._intent.sessionId }}", "text": "The time is
        {{ now().hour }} hundred"}
  {% elif strptime(minutes, '%M') < strptime('10', '%M') %}
      payload_template: >-
        {"sessionId": "{{ trigger.event.data._intent.sessionId }}", "text": "The time is
        {{ now().hour }} oh {{ now().minute }} minutes"}
    {% else %}
      payload_template: >-
        {"sessionId": "{{ trigger.event.data._intent.sessionId }}", "text": "The time is
        {{ now().hour }} {{ now().minute }} minutes"}
    {% endif %}

but I get an error loading the automation. Where did I go wrong?

I’m thinking I might have to move the formatting to a script. Do I need to run set, if/then, etc. in a script and how would I pass the results back to the automation service mqtt.publish? Thanks for any help.

You can’t control the YAML keys with templates; only the values of keys/fields. In other words, have a single payload_template and put all your logic under that.

But I don’t think you need that at all. Try this in your template editor (not sure if you intended to have a space between hour and minute in your first snippet):

{{ now().strftime("%H:%M") }}

https://strftime.org/

Thank you for helping. I tried putting the logic under payload_template, like so:

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

but this throws an error and will not load. The error is that “template value should be a string” but I don’t see how I will do the comparisons if one side is a string.

In plain language, I want the automation to test the value of now().minute. If it is zero, use one sessionId… If it is greater than zero but less than ten, use another sessionId. If it is ten or more, use a third sessionId. Sample outputs would be “18 hundred”, “18 zero nine”, or “18 14”.

service: mqtt.publish
data:
  topic: hermes/dialogueManager/endSession
  payload_template: >-
    {% set m = now().minute %}
    {"sessionId": "{{ trigger.event.data._intent.sessionId }}", 
    "text": "The time is {{ now().hour }} {{ 'hundred' if m == 0 else 'zero ' ~ m if m < 10 else m }}"}

In case you’re wondering why errors were reported for your version, it’s because it contains several Jinja2 syntax errors.

The following is incorrect because it attempts to put a template within another template and it compares a string (the output of strftime) to an integer (the value zero).

{% if {{now().strftime("%M")}} == 0 %}

The same mistakes are made for the elif statement.

In addition, the first character in this line appears to be a typo:

    (% else %}

It should be an opening brace { not an opening parenthesis.

The final {% endif %} is missing (although that might just be a copy-paste error).

I found that piping that function to int works, after correcting the other errors. I have neuropathy in my right hand so I make a lot of typos.

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

It seems like there should be a function that returns integers from time directly. Thanks for your help.

There is, I used it in the example I posted above to get minutes as an integer value.

In addition, the example is far more concise because it avoids needless repetition of the entire string; only the parts of the string that can change are templated.

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information, refer to guideline 21 in the FAQ.