script only works after reset

The script below is connect to a inductive sensor with a voltage divider
When the arduino boots it isnt running.

After pressing the reset button it's running well.

Any suggestions?

/*
 Basic MQTT example with Authentication

  - connects to an MQTT server, providing username
    and password
  - publishes "hello world" to the topic "outTopic"
  - subscribes to the topic "inTopic"
*/

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// set pin numbers:
const int pin1 = 3;     // the number of the pushbutton pin

const int ledPin =  13;      // the number of the LED pin


// variables will change:
int sensor1 = 0;         // variable for reading the pushbutton status

int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 5, 210);
IPAddress server(192, 168, 5, 204);


void callback(char* topic, byte* payload, unsigned int length) {
 // Serial.print("Message arrived [");
 // Serial.print(topic);
 // Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

void setup()
{
  // initialize serial communication:
  Serial.begin(9600);
  Serial.println("start");
    // initialize the LED pin as an output:
  //pinMode(ledPin, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(sensor1, INPUT);
  
  
  Ethernet.begin(mac, ip);
  // Note - the default maximum packet size is 128 bytes. If the
  // combined length of clientId, username and password exceed this,
  // you will need to increase the value of MQTT_MAX_PACKET_SIZE in
  // PubSubClient.h
  
  if (client.connect("arduinoClient", "", "")) {
    client.publish("biertap","hello world");
    client.subscribe("biertap");
  }
}


void loop() {
  client.loop();

  sensor1 = digitalRead(pin1);
    if (sensor1 == LOW ) {
    buttonState = HIGH;
    } else {
    buttonState = LOW;
    }

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      Serial.println("biertap actie");
      client.publish("biertap","actie");
      digitalWrite(LED_BUILTIN, HIGH);
      
      
    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      digitalWrite(LED_BUILTIN, LOW);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
  
}
  // initialize the pushbutton pin as an input:
  pinMode(sensor1, INPUT);

So, you didn't order a generic sensor. Why such a generic name for the pin variable?

As proof that it is a stupid name, you gave it a value of 0, but later, you read from pin1.

  if (client.connect("arduinoClient", "", "")) {
    client.publish("biertap","hello world");
    client.subscribe("biertap");
  }
}

Does client.connect() block until the connection happens? Or does it time out after some short time?

thanks!

is it better then to delete the part below?

  if (client.connect("arduinoClient", "", "")) {
    client.publish("biertap","hello world");
    client.subscribe("biertap");
  }
}

is it better then to delete the part below?

Better than what?

Post a link to the non-standard library (or libraries) you are using.

You might need to do that in a while loop, waiting for the connection to happen, or for some number of attempts to fail.

I found the library on the link below

I think that the first thing you need to do is print whether the function returns true or false when the Arduino is initially started, and when it is reset.