# How do I make use of gen\_statem(enterstate, L

**URL:** <https://erlangforums.com/t/how-do-i-make-use-of-gen-statem-enterstate-l/5298>\
**Category:** Questions / Help\
**Tags:** gen\_statem\
**Created:** [December 3, 2025, 11:46am UTC](https://erlangforums.com/t/how-do-i-make-use-of-gen-statem-enterstate-l/5298 "2025-12-03T11:46:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paddlaren](https://erlangforums.com/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@Paddlaren](https://erlangforums.com/u/Paddlaren)\
**Post date:** [December 3, 2025, 11:46am UTC](https://erlangforums.com/t/how-do-i-make-use-of-gen-statem-enterstate-l/5298/1 "2025-12-03T11:46:10Z")

</div>

Hi,

I am building finite state machines and would like to enter a state function directly when transition is made to the state.

In the [documentation](https://www.erlang.org/doc/apps/stdlib/gen_statem.html#c:StateName/3) I found:

> ‘StateName’(enter, OldStateName :: state\_name(), data())

When I make such a state function for a state, it never enters that state function. Only signal driven state functions are called.

BTW, I use:

```erlang
callback_mode() ->
    state_functions.

```

I can’t really find out more information on this (or am I just blind?). What is the purpose of this mechanism and how do I use it?

Cheers, Erik

---

<div class="post-metadata">

**Author:** ![svilen](https://erlangforums.com/letter_avatar_proxy/v4/letter/s/41988e/32.png) [@svilen](https://erlangforums.com/u/svilen)\
**Post date:** [December 3, 2025, 2:11pm UTC](https://erlangforums.com/t/how-do-i-make-use-of-gen-statem-enterstate-l/5298/2 "2025-12-03T14:11:12Z")

</div>

Try

```erlang
callback_mode() ->
    [state_functions, state_enter].

```

It is described in [https://www.erlang.org/doc/apps/stdlib/gen\_statem.html#t:state\_enter/0](https://www.erlang.org/doc/apps/stdlib/gen_statem.html#t:state_enter/0)

Svilen

---

<div class="post-metadata">

**Author:** ![vances](https://erlangforums.com/user_avatar/erlangforums.com/vances/32/710_2.png) [@vances](https://erlangforums.com/u/vances)\
**Post date:** [December 3, 2025, 11:53pm UTC](https://erlangforums.com/t/how-do-i-make-use-of-gen-statem-enterstate-l/5298/3 "2025-12-03T23:53:06Z")

</div>

Here’s a working example:

```erlang
-module(foo_fsm).

-behaviour(gen_statem).

-export([init/1, handle_event/4, callback_mode/0,
        terminate/3, code_change/4]).
-export([null/3]).

callback_mode() ->
    [state_functions, state_enter].

init(_Args) ->
    process_flag(trap_exit, true),
    {ok, null, []}.

null(enter, OldStateName, Data) ->
    io:fwrite("~w:~w(~w, ~w, ~w)~n",
            [?MODULE, ?FUNCTION_NAME, enter, OldStateName, Data]),
    keep_state_and_data;
null(EventType, EventContent, Data) ->
    io:fwrite("~w:~w(~w, ~w, ~w)~n",
            [?MODULE, ?FUNCTION_NAME, EventType, EventContent, Data]),
    keep_state_and_data.

handle_event(_EventType, _EventContent, _State, _Data) ->
    keep_state_and_data.

terminate(_Reason, _State, _Data) ->
    ok.

code_change(_OldVsn, OldState, OldData, _Extra) ->
    {ok, OldState, OldData}.

```

This will print the called callback functions and arguments:

```erlang
1> {ok, Pid} = gen_statem:start(foo_fsm, [], []).
foo_fsm:null(enter, null, [])
{ok,<0.86.0>}
2> gen_statem:cast(Pid, bar).
foo_fsm:null(cast, bar, [])
ok

```

---

<div class="post-metadata">

**Author:** ![Paddlaren](https://erlangforums.com/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@Paddlaren](https://erlangforums.com/u/Paddlaren)\
**Post date:** [December 5, 2025, 7:50am UTC](https://erlangforums.com/t/how-do-i-make-use-of-gen-statem-enterstate-l/5298/4 "2025-12-05T07:50:55Z")

</div>

It was really so simple… And it works like a charm now!

Thanks! 😀
