# Another dsm question - same trigger different transitions

**URL:** https://discourse.nodered.org/t/another-dsm-question-same-trigger-different-transitions/4768
**Category:** General
**Created:** [11 November 2018 15:15 UTC](https://discourse.nodered.org/t/another-dsm-question-same-trigger-different-transitions/4768 "2018-11-11T15:15:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 November 2018 15:15 UTC](https://discourse.nodered.org/t/another-dsm-question-same-trigger-different-transitions/4768/1 "2018-11-11T15:15:45Z")

</div>

Looking for advice on the best way to accomplish something in node-red-contrib-dsm.  
I have a situation where the same trigger input causes different transitions dependent on current state, which is not in itself a problem. The issue is that as well as causing different transitions I wish other aspects of the transition to be different. For example I have attached a simple flow where the trigger causes the state machine to step through states 1, 2, 3 and then back to 1. I wish to suppress the output except when going from state 3 to state 1. I have accomplished this by testing `sm.currentState` in the trigger method and this does work, but it is not in keeping with the philosophy of a state machine so I suspect there may be a better way. This is a very much simplified example of what I am actually trying to achieve of course.  
On a side issue it is a little confusing that in the transition method `sm.currentState` is already on the new state, but that is a different issue.

```auto
[{"id":"a60e15e3.e62a18","type":"dsm","z":"5dfa5b0c.04132c","name":"dsm test","sm_config":"{\n \"currentState\": \"state1\",\n \"states\": {\n \"state1\": {\n \"trigger\": \"state2\"\n },\n \"state2\": {\n \"trigger\": \"state3\"\n },\n \"state3\": {\n \"trigger\": \"state1\"\n }\n },\n \"methods\": {\n \"trigger\": [\n \"if (sm.currentState != 'state1')\",\n \"output = false;\"\n]\n }\n}","x":269,"y":759,"wires":[["2ffcc5d4.eced12"]]},{"id":"8b45332e.85d5e8","type":"inject","z":"5dfa5b0c.04132c","name":"trigger","topic":"trigger","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":107.5,"y":759,"wires":[["a60e15e3.e62a18"]]},{"id":"2ffcc5d4.eced12","type":"debug","z":"5dfa5b0c.04132c","name":"","active":true,"console":"false","complete":"true","x":423,"y":759,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)
#### Post date: [11 November 2018 16:57 UTC](https://discourse.nodered.org/t/another-dsm-question-same-trigger-different-transitions/4768/2 "2018-11-11T16:57:16Z")

</div>

The current version doesn't support state-methods.

Here is a workaround:

1. the state will change before the transition method is executed. So if you want to test the actualState before the transition, you could use the `onBeforeTransition` method and save true/false in a sm variable (sm.out)
2. in the `trigger` method use sm.out to set the output.

```auto
{
    "currentState": "state1",
    "states": {
        "state1": {
            "trigger": "state2"
        },
        "state2": {
            "trigger": "state3"
        },
        "state3": {
            "trigger": "state1"
        }
    },
    "methods": {
        "onBeforeTransition": [
            "sm.out = sm.currentState === 'state3' ? true : false;"
        ],
        "trigger": [
            "output = sm.out;",
            "/* your method code */;"
        ]
    }
}

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 November 2018 17:33 UTC](https://discourse.nodered.org/t/another-dsm-question-same-trigger-different-transitions/4768/3 "2018-11-11T17:33:35Z")

</div>

OK, thanks. It isn't actually an issue testing the new state as (in this case anyway) that is sufficient to decide what to do. It was more the fact that it just didn't seem right to be running code based on the state. Nothing is perfect of course.

On a different subject, have you considered allowing multiple outputs. I seem to end up with a switch on the output in order to determine where to send the message and multiple outputs would save that.  
`output = 2`  
for example.

---

<div class="post-metadata">

### Author: ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)
#### Post date: [12 November 2018 11:05 UTC](https://discourse.nodered.org/t/another-dsm-question-same-trigger-different-transitions/4768/4 "2018-11-12T11:05:08Z")

</div>

Multiple outputs is on my todo list but with a low priority.

I agree, it would avoid an additional node but In this case adding a switch node to the output is quite easy to do.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [12 November 2018 12:34 UTC](https://discourse.nodered.org/t/another-dsm-question-same-trigger-different-transitions/4768/5 "2018-11-12T12:34:35Z")

</div>

> [@cflurin](#):
>
> Multiple outputs is on my todo list

🙂

> [@cflurin](#):
>
> but with a low priority

☹

Though it is not a major issue I agree.

---

<div class="post-metadata">

### Author: ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)
#### Post date: [20 November 2018 12:12 UTC](https://discourse.nodered.org/t/another-dsm-question-same-trigger-different-transitions/4768/6 "2018-11-20T12:12:44Z")

</div>

With the new version 0.13.0 you can now use `preState`:

```auto
{
    "stateOutput": "current",
    "preStateOutput": "previous",
    "currentState": "state1",
    "states": {
        "state1": {
            "trigger": "state2"
        },
        "state2": {
            "trigger": "state3"
        },
        "state3": {
            "trigger": "state1"
        }
    },
    "methods": {
        "trigger": [
            "output = sm.preState === 'state3' ? true : false;"
        ]
    }
}
```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [20 November 2018 13:39 UTC](https://discourse.nodered.org/t/another-dsm-question-same-trigger-different-transitions/4768/7 "2018-11-20T13:39:44Z")

</div>

OK, thanks
