Why y nodemcu code for PIR is not working?

OK. I can't explain why the attempt to write only to field 2 failed.

Anyway, it looks like you should do something like this:

ThingSpeak.setField( (unsigned int) 1, (int) val ) ;          // store field 1   
ThingSpeak.setField( (unsigned int) 2, (int) motionState ) ;  // store field 2
ThingSpeak.writeFields(myChannelNumber, apiKey);   // write all fields to the channel and reset stored fields