Get parent of current page

Try using get_post_ancestors. Here is how you can apply this in your case:

<?php
    global $wp_query;
    $post = $wp_query->post;
    $ancestors = get_post_ancestors($post);
    if( empty($post->post_parent) ) {
        $parent = $post->ID;
    } else {
        $parent = end($ancestors);
    } 
    if(wp_list_pages("title_li=&child_of=$parent&echo=0" )) {                
        wp_list_pages("title_li=&child_of=$parent&depth=1" ); 
    } 
?>

You’ll probably need to remove the depth parameters to show you’re 3rd level pages.

Let me know if this helps!

error code: 523