Handle Numbers that are recognized as words

If I set up an automation triggered by a sentence that includes a number, sometimes assist will represent the number with the English word (e.g. “two” instead of “2”). The former can’t be fed into numerical operations in templates (like int to parse the string as a number to use for math/setting numerical values).

Is there a way to work around this? Is there a markup in the sentence trigger that I can use to express a particular input should always be digits?

Or in my jinja template that parses the assist input, can I parse the English words into digits there instead? (I’m pretty sure this is an option, just need to find the right templating.)

Most of the time the trigger slot should return a numeral, but you can use a custom macro like this one to insure that you get an integer instead of a number name.

Not in a Sentence Trigger, but it can be done in a Custom Sentence and Intent Script. Doing it this way is a little more complicated, but it gives you more control. Forum member jackjourneyman has a nice tutorial on github.

Thanks! I ended up writing a similar macro:

{% macro parse_number_word(input) %}
{% if input == 'one' %}
  1
{% elif input == 'two' %}
  2
{% elif input == 'three' %}
  3
{% elif input == 'four' %}
  4
{% elif input == 'five' %}
  5
{% elif input == 'six' %}
  6
{% elif input == 'seven' %}
  7
{% elif input == 'eight' %}
  8
{% elif input == 'nine' %}
  9
{% elif input == 'ten' %}
  10
{% elif input == 'eleven' %}
  11
{% elif input == 'twelve' %}
  12
{% else %}
  {{ input }}
{% endif %}
{% endmacro %}

(Recognizing all numbers would be great, but these were enough for what I was doing.)

It does feel a bit like the “I wish there was an easier way to do this” meme about working out if a number is even or not using only if/else, but it works!

I’ll have to look into custom sentences and intents in more depth some time, that sounds like it could be better.