Using counters in Conversation.

In Watson Dialog the use of counters was pretty easy. In Conversation it is as well, but not as intuitive yet. Here I am going to explore three methods of building and using a counter.

First to create a counter, you need to go to advanced mode in your node. You can then type in something like the following.

  "output": {
    "text": "Keep saying T1"
  "context": {
    "countdown": 3

A common mistake that people make is to forget to add the comma after the output block. Conversation will normally make the editor window red, but doesn’t give any hints if you do this. Worse still, if you don’t notice the red block then Conversation will revert that node if you attempt to test or edit another node.

After this we create a node with the following output.

Counter is at: <? context.countdown-- ?>

This tells conversation to display the counter we made, then decrement it. Within the nodes condition we can check to ensure the counter is greater than 0, if not then move to the next node to tell them you are finished. For example:


Hiding the counter

Now you may not want the end user to be able to see the counter. In which case we need to mask the executing code from being rendered. To do this we create a node with the code to execute as follows:

<? context.countdown-- ?>

You then set this node to continue from another node, linking to the output (very important!). In the second node, you put the following into the advanced output window.

  "output": {
    "text": {
      "append": false,
      "values": [
        "Still counting down."

What this does is generate output “Still counting down.”, but because “append” is false, it will erase the previous output. So the user only sees this message.


Do you really need a counter?

The simple solution can be to not use a counter at all. The following code for example will simulate the previous example by just cycling through custom responses.

  "output": {
    "text": {
      "values": [
        "Keep saying T3.",
        "Counting down 3",
        "Counting down 2",
        "Counting down 1",
        "Counting finished!"
      "selection_policy": "sequential"

Here is the sample conversation script you can play with.

Important reminder!

If you use the counter in your conversation flow, you should be responsible for it! It is good coding practice to declare it in the conversation.

Apart from being able to test, it allows you to see where the application layer would likely set the counter. Plus if something failed at the application layer you can trap easier.

Depending on the complexity of your conversational flow, you may sometimes want the application layer to handle modifying the counter. This is fine as well, but still initialize in conversation, even if the application overwrites that initialization.


3 thoughts on “Using counters in Conversation.

  1. mingze says:

    Another way to count in the context instead of in the output:

    "context": { "countdown": "<? $countdown -1 ?>" }
    "context": { "countdown": "<? $countdown-- ?>" }

  2. sandy says:

    Awesome blog. I have question:

    In the values defined inside the text , how to use a condition statement and direct new flow.
    I mean when the value is Counting finished! , then I want to show some text which is an another node.
    Do you have any suggestion how to achieve that. ?

Leave a Reply